E - Cover it!

Graph coloring problem, that is, when traversing the graph, the game that requires minimal staining point, so nothing thinking, I discovered that there is no requirement that traverse directly on the line. Data is large, need to be optimized dfs, to not TTTTTTTTT!

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int t;
 4 const int MAXN=2e5+10;
 5 vector <int>V[MAXN],ans[2]; 
 6 int m,n;
 7 int vis[MAXN];
 8 void dfs(int u,int c)
 9 {
10     vis[u]=1;ans[c].push_back(u);
11     for(int i=0;i<V[u].size();i++)
12     {
13         if(vis[V[u][i]]==1) continue;
14         dfs(V[u][i],c^1);
15     }
16     return;
17 }
18 
19 int main()
20 {
21     int T;
22     scanf("%d",&T);
23     while(T--) {
24         scanf("%d%d",&n,&m);
25         for(int i=1;i<=n;++i) {V[i].clear();vis[i]=0;}
26         ans[0].clear();ans[1].clear();
27         for(int i=1;i<=m;++i) {
28             int a,b;
29             scanf("%d%d",&a,&b);
30             V[a].push_back(b);
31             V[b].push_back(a);
32         }
33         dfs(1,0);
34         printf("%d\n",min(ans[0].size(),ans[1].size()));
35         if(ans[0].size()>ans[1].size()) swap(ans[0],ans[1]);
36         for(int i=0;i<ans[0].size();i++)    
37             printf("%d ",ans[0][i]);
38         printf("\n");
39     }
40     return 0;
41 }

 

Guess you like

Origin www.cnblogs.com/Msmw/p/11039063.html