Submission #233862

#TimeUsernameProblemLanguageResultExecution timeMemory
233862LittleFlowers__Network (BOI15_net)C++17
100 / 100
1089 ms91884 KiB
#include <bits/stdc++.h> using namespace std; #define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;}) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //mt19937 rng(1012); int rnd(int l,int r){return l+rng()%(r-l+1);} #define fasty ios_base::sync_with_stdio(0),cin.tie(0); #define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a) #define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a) #define forv(a,b) for(auto&a:b) #define fi first #define se second #define pb push_back #define ii pair<int,int> #define mt make_tuple #define all(a) a.begin(),a.end() #define reset(f, x) memset(f, x, sizeof(f)) #define bit(x,i) ((x>>(i-1))&1) #define on(x,i) (x|(1ll<<(i-1))) #define off(x,i) (x&~(1<<(i-1))) #define gg exit(0); const int N=500010; int n; vector<int> val; vector<int> ad[N],adj[N]; vector<ii> ans; void add(int i,int j){ adj[i].pb(j); adj[j].pb(i); ans.pb({i,j}); } int it,top,scc; int st[N],ed[N],co[N],w[N],tr[N]; void dfs1(int u){ st[u]=ed[u]=++it; co[u]=1; w[++top]=u; forv(v,ad[u]) if(v!=tr[u]){ if(co[v]==0){ tr[v]=u; dfs1(v); ed[u]=min(ed[u],ed[v]); } else if(co[v]==1){ ed[u]=min(ed[u],st[v]); } } forv(v,adj[u]) if(v!=tr[u]){ if(co[v]==0){ tr[v]=u; dfs1(v); ed[u]=min(ed[u],ed[v]); } else if(co[v]==1){ ed[u]=min(ed[u],st[v]); } } if(st[u]==ed[u]){ scc++; int v; do{ v=w[top--]; co[v]=2; } while(u!=v); } } main(){ #define task "network" //freopen(task".inp","r",stdin); //freopen(task".out","w",stdout); n=in; forinc(i,2,n){ int u=in,v=in; ad[u].pb(v); ad[v].pb(u); } forinc(i,1,n) if(ad[i].size()==1) val.push_back(i); for(;;){ shuffle(all(val),rng); for(int l=0;l+1<val.size();l+=2) add(val[l],val[l+1]); if(val.size()&1) add(val[0],val.back()); dfs1(1); if(scc==1) break; ans.clear(); it=top=scc=0; forinc(i,1,n){ co[i]=tr[i]=0; adj[i].clear(); } } //cout<<1; //gg cout<<ans.size()<<"\n"; forv(i,ans) cout<<i.fi<<" "<<i.se<<"\n"; }

Compilation message (stderr)

net.cpp:69:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
net.cpp: In function 'int main()':
net.cpp:84:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int l=0;l+1<val.size();l+=2)
                     ~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...