Submission #609974

#TimeUsernameProblemLanguageResultExecution timeMemory
609974aSSSdNetwork (BOI15_net)C++14
100 / 100
899 ms138172 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()); int rnd(int l,int r) { return l+rng()%(r-l+1); } #define fasty ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); #define forinc(x,a,b) for(int x=a;x<=b;x++) #define fordec(x,a,b) for(int x=a;x>=b;x--) #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 getbit(x,i) ((x>>i)&1) #define batbit(x,i) (x|(1ll<<i)) #define tatbit(x,i) (x&~(1<<i)) #define gg exit(0); const int N = 5e5 + 100; vector<int> ke[N]; set<int> adj[N]; int n, m; int num[N], low[N], it, del[N]; stack<int> st; int cnt; int id[N], two[N]; void dfs(int u,int p) { num[u]=low[u]=++it; st.push(u); for(auto v:ke[u]) { if(del[v]) continue; if(v==p) continue; if(!num[v]) { dfs(v,u); low[u]=min(low[u],low[v]); } else low[u]=min(low[u],num[v]); } if(num[u]==low[u]) { cnt++; int v; while(1) { v=st.top(); st.pop(); id[v]=cnt; del[v]=1; two[cnt] = v; if(v==u) break; } } } vector<int> leaf; void dfs2(int u, int pa) { if(adj[u].size() == 1) leaf.pb(two[u]); for(auto v : adj[u]) { if(v == pa) continue; dfs2(v,u); } } main() { fasty; // freopen("bluehouse.inp" , "r" ,stdin); // freopen("bluehouse.out" , "w", stdout ); // freopen("task.inp" , "r", stdin); cin >> n; m=n-1; forinc(i,1,m) { int u,v; cin >> u >> v; ke[u].pb(v); ke[v].pb(u); } forinc(i,1,n) if(!num[i]) dfs(i,0); forinc(i,1,n) { for(auto v : ke[i]) { int x = id[i]; int y = id[v]; if(x!=y) { adj[x].insert(y); adj[y].insert(x); } } } dfs2(1,0); int l=0; int step = leaf.size()/2; if(leaf.size() % 2 == 0) { cout << leaf.size()/2 << "\n"; while(l + leaf.size()/2 < leaf.size()) { cout << leaf[l] << " " << leaf[l + step] << "\n"; l++; } return 0; } leaf.pb(leaf[0]); step++; cout << leaf.size()/2 << "\n"; while(l + step < leaf.size()) { cout << leaf[l] << " " << leaf[l + step] << "\n"; l++; } }

Compilation message (stderr)

net.cpp:75:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   75 | main()
      | ^~~~
net.cpp: In function 'int main()':
net.cpp:122:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |     while(l + step < leaf.size())
      |           ~~~~~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...