标签:cout txt stdin include put can return ret input
#include <iostream> using namespace std; struct chess{ int x; }; int color[1000]={0}; int maxnum; int data[1000][1000]={0}; chess p[1000]; int flag; void bfs(int n); int main() { freopen("input.txt","r",stdin); int n; int pair; int x,y; while(scanf("%d",&n)!=EOF) { cin >>pair; maxnum=0; for(int i=1;i<=n;i++) color[i]=0; for(int i=1;i<=n;i++) for(int j=0;j<n;j++) data[i][j]=0; for(int i=0;i<pair;i++) { cin >>x; cin >>y; data[x][y]=1; data[y][x]=1; } bfs(n); if(flag==1) maxnum=-1; else { for(int i=1;i<=n;i++) { if(color[i]==1) { maxnum++; cout <<i<<" "; } } cout <<endl; } cout <<maxnum<<endl; } return 0; } void bfs(int n) { int front=0; int rear=0; p[rear++].x=1; color[p[front].x]=1; while(rear>front) { flag=0; int nx=p[front].x; for(int i=1;i<=n;i++) { if(data[nx][i]==1) { if(color[i]==0) { color[i]-=color[nx]; p[rear++].x=i; } else if(color[nx]==color[i]) { flag=1; break; } } } if(flag==1) break; front++; } }
标签:cout txt stdin include put can return ret input
原文地址:http://www.cnblogs.com/jintg/p/6339771.html