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
| #include<cstdio> #include<vector> #include<cstring> using namespace std; int n,m; int min1=99999999; vector < int > a[99999]; int ans; int ans1; int f[99999]; int flag; int dfs(int x) { if(flag==1) return 0; for(int i=0;i<a[x].size();i++) { if(f[a[x][i]]==f[x]) { flag=1;return 0; } else if(!f[a[x][i]]) { if(f[x]==1) f[a[x][i]]=2,ans1++; else if(f[x]==2) f[a[x][i]]=1,ans++; dfs(a[x][i]); } } return 0; }int tot=0; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); a[x].push_back(y); a[y].push_back(x); } for(int i=1;i<=n;i++) {ans=0; ans1=0; if(!f[i]) { ans++;//ans1++; f[i]=1; //flag[i]=1; dfs(i); tot+=min(ans,ans1); } //min1=min(tot,min1); } if(flag==1)printf("Impossible"); else printf("%d",tot); return 0; }
|