这是由AC代码改的模板,不能直接交啊
#include<iostream> #include<vector> #include<cstring> using namespace std; int book[2024]; vector<int>v[2048]; bool dfs(int x,int c) { book[x]=c; for(int i=0;i<v[x].size();i++){ if(book[v[x][i]]==c){return false;} if(book[v[x][i]]==0&&!dfs(v[x][i],-c)){return false;} } return true; } int main() { int T; cin>>T; int n,m,x,y; while(T--){ memset(book,0,sizeof(book)); cin>>n>>m; for(int i=0;i<m;i++){ cin>>x>>y; v[x].push_back(y); v[y].push_back(x); } int flag=0; for(int i=1;i<=n;i++){ if(!book[i]&&!dfs(i,1)){flag=1;break;} } if(flag){cout<<"非二分图"<<endl;} else cout<<"二分图"<<endl; for(int i=0;i<=n;i++){ v[i]=vector<int>(); } } }