Codeforces Round #649 (Div. 2)D. Ehab's Last Corollary(环与独立集)
题:https://codeforces.com/contest/1364/problem/D
题意:给出n个点m条边的图,找到节点数小于等于k的环或节点数等于(k+1)/2的独立集;
分析:先找环,找不到环证明一定存在有独立集,独立集的找法就是01染色。
#include<bits/stdc++.h> using namespace std; #define pb push_back const int M=1e5+5; int flag,vis[M],book[M],deep[M],fa[M],tmp; vector<int>ans2,ans1,g[M]; void dfs(int u){ ///cout<<u<<endl; if(flag) return; vis[u]=1; for(auto v:g[u]){ if(flag) break; ///cout<<v<<"!!"<<endl; if(v==fa[u]) continue; if(vis[v]){ ///cout<<u<<"!!"<<v<<endl; /// cout<<deep[u]-deep[v]+1<<"!!"<<endl; if(deep[u]>deep[v]&&deep[u]-deep[v]+1<=tmp){ for(int i=u;i!=fa[v];i=fa[i]) ans2.pb(i); flag=1; break; } } else{ deep[v]=deep[u]+1; fa[v]=u; dfs(v); } } if(!book[u]){ ans1.pb(u); for(auto v:g[u]){ book[v]=1; } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); int n,m,k; cin>>n>>m>>k; tmp=k; for(int u,v,i=1;i<=m;i++){ cin>>u>>v; g[u].pb(v); g[v].pb(u); }///cout<<"!!"<<endl; dfs(1); if(flag){ cout<<2<<endl; cout<<ans2.size()<<endl; for(auto v:ans2) cout<<v<<' '; } else{///du cout<<1<<endl; int L=(tmp+1)/2; for(int i=0;i<L;i++) cout<<ans1[i]<<' '; } return 0; }