1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include<bits/stdc++.h> using namespace std; typedef long long ll; #define IOS ios_base::sync_with_stdio(false);cin.tie(0); #define maxn 105 #define for_(i,n) for(ll (i)=1;(i)<=(n);i++) struct edge{ int u,v,w; }; vector<edge> e; set<int> ans; int f[maxn],enemy[maxn]; int findfa(int x){ if(x==f[x])return x; else { f[x]=findfa(f[x]); return f[x]; } } void mergefa(int a,int b){ int fa=findfa(a),fb=findfa(b); if(fa==fb)return ; else{ f[fa]=fb; return ; } } vector<int> g[maxn]; int col[maxn]; void dfs(int x,int fa,int co){ col[x]=co; for(auto i:g[x]){ if(i!=fa&&col[i]==0)dfs(i,x,co); } } int main(){ IOS int n,m; cin>>n>>m; while(m--){ int u,v,w; cin>>u>>v>>w; g[u].push_back(v); g[v].push_back(u); e.push_back({u,v,w}); } for_(i,n){ f[i]=i; if(!col[i]){dfs(i,0,i);} } for(auto i:e){ int u=i.u,v=i.v; if(i.w==1){ mergefa(i.u,i.v); }else{ if(findfa(u)==findfa(v)){ cout<<"Impossible"; return 0; } if(!enemy[u])enemy[u]=findfa(v); else mergefa(enemy[u],v); if(!enemy[v])enemy[v]=findfa(u); else mergefa(enemy[v],u); } } for_(i,n){ for_(j,n){ if(findfa(i)==findfa(j)&&enemy[i]!=0&&enemy[j]!=0){ mergefa(enemy[i],enemy[j]); } } } for_(i,n)ans.insert(col[i]); set<int> sum; for(auto i:ans){ int fa=findfa(i); for_(j,n){ if(col[j]!=i)continue; if(findfa(j)==fa){sum.insert(j);} } } cout<<sum.size()<<endl; for(auto i:sum)cout<<i<<" "; return 0; }
|