Submission #321155

# Submission time Handle Problem Language Result Execution time Memory
321155 2020-11-11T08:33:53 Z AmineWeslati Network (BOI15_net) C++14
Compilation error
0 ms 0 KB
//Never stop trying
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef long long ll;
#define int ll
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<str> vs;
typedef vector<ld> vd;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)

const int MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const int MX = 2e5 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up

template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

ll random(ll a, ll b){
    return a + rng() % (b - a + 1);
}

#ifndef LOCAL  
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl; 
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;

void IO() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int N; 
vpi adj[MX];

bool finished(){
	FOR(u,1,N+1) for(auto ed: adj[u]) if(ed.se==1) return 0;
	return 1; 
}

int val[MX],mxVal[MX];
void dfs(int u, int p){
	val[u]=0,mxVal[u]=0;
	vi vec;
	for(auto v: adj[u]) if(v.fi!=p){
		dfs(v.fi,u);
		int x=mxVal[v.fi]+v.se;
		vec.pb(x);
		ckmax(mxVal[u],x);
	}

	sort(rall(vec));
	if(sz(vec)) val[u]=vec[0];
	if(sz(vec)>1) val[u]+=vec[1];
}

int32_t main() {
    boost; IO();

    cin>>N;
    FOR(i,0,N-1){
    	int u,v; cin>>u>>v;
    	adj[u].pb({v,1});
    	adj[v].pb({u,1});
    }
    dfs(1,1);
    //FOR(i,1,N+1) cout << val[i] << endl;

    int ans=0;
    vpi res;
    while(!finished()){
    	dfs(1,1);
    	int mx=-1,u;
    	FOR(i,1,N+1) if(ckmax(mx,val[i])) u=i;
    	//cout << u << endl;
    	//FOR(i,1,N+1) cout << val[i] << ' ' << mxVal[i] << endl;

    	int a=u,b=u,pa=-1,pb=-1;
    	bool taken[N+1]={false};
    	while(sz(adj[a])>1 || a==1){
    		int mx=-1,nxt;
    		for(auto v: adj[a]) if(v.fi!=pa && ckmax(mx,mxVal[v.fi]+v.se)) nxt=v.fi;
    		//dbg(nxt);
    		for(auto &v: adj[a]) if(v.fi==nxt) v.se=0;
    		for(auto &v: adj[nxt]) if(v.fi==a) v.se=0;
    		taken[nxt]=true;
    		pa=a; a=nxt; 
    	}

    	//for(auto v: adj[1]) cout << v.se << endl;

    	dfs(1,1);
    	/*cout << endl;
    	FOR(i,1,N+1) cout << val[i] << ' ' << mxVal[i] << endl;*/
    	while(sz(adj[b])>1){
    		int mx=-1,nxt;
    		for(auto v: adj[b]) 
    			if(v.fi!=pb && !taken[v.fi] && ckmax(mx,mxVal[v.fi]+v.se)) 
    				nxt=v.fi;
			if(mx==-1) break;
    		for(auto &v: adj[b]) if(v.fi==nxt) v.se=0;
    		for(auto &v: adj[nxt]) if(v.fi==b) v.se=0;
    		pb=b; b=nxt; 
    	}

    	ans++;
    	res.pb({a,b});
    	/*dbg(u);
    	dbgs(a,b);*/
    }

    cout << ans << endl;
    for(auto x: res) cout << x.fi << ' ' << x.se << endl;
    

    return 0;
}

/* Careful!!!
    .Array bounds
    .Infinite loops
    .Uninitialized variables / empty containers
    .Multisets are shit

   Some insights:
    .Binary search
    .Graph representation
    .Write brute force code
    .Change your approach
*///Never stop trying
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef long long ll;
#define int ll
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<str> vs;
typedef vector<ld> vd;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)

const int MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const int MX = 2e5 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up

template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

ll random(ll a, ll b){
    return a + rng() % (b - a + 1);
}

#ifndef LOCAL  
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl; 
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;

void IO() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int N; 
vpi adj[MX];

bool finished(){
	FOR(u,1,N+1) for(auto ed: adj[u]) if(ed.se==1) return 0;
	return 1; 
}

int val[MX],mxVal[MX];
void dfs(int u, int p){
	val[u]=0,mxVal[u]=0;
	vi vec;
	for(auto v: adj[u]) if(v.fi!=p){
		dfs(v.fi,u);
		int x=mxVal[v.fi]+v.se;
		vec.pb(x);
		ckmax(mxVal[u],x);
	}

	sort(rall(vec));
	if(sz(vec)) val[u]=vec[0];
	if(sz(vec)>1) val[u]+=vec[1];
}

int32_t main() {
    boost; IO();

    cin>>N;
    FOR(i,0,N-1){
    	int u,v; cin>>u>>v;
    	adj[u].pb({v,1});
    	adj[v].pb({u,1});
    }
    dfs(1,1);
    //FOR(i,1,N+1) cout << val[i] << endl;

    int ans=0;
    vpi res;
    while(!finished()){
    	dfs(1,1);
    	int mx=-1,u;
    	FOR(i,1,N+1) if(ckmax(mx,val[i])) u=i;
    	//cout << u << endl;
    	//FOR(i,1,N+1) cout << val[i] << ' ' << mxVal[i] << endl;

    	int a=u,b=u,pa=-1,pb=-1;
    	bool taken[N+1]={false};
    	while(sz(adj[a])>1 || a==1){
    		int mx=-1,nxt;
    		for(auto v: adj[a]) if(v.fi!=pa && ckmax(mx,mxVal[v.fi]+v.se)) nxt=v.fi;
    		//dbg(nxt);
    		for(auto &v: adj[a]) if(v.fi==nxt) v.se=0;
    		for(auto &v: adj[nxt]) if(v.fi==a) v.se=0;
    		taken[nxt]=true;
    		pa=a; a=nxt; 
    	}

    	//for(auto v: adj[1]) cout << v.se << endl;

    	dfs(1,1);
    	/*cout << endl;
    	FOR(i,1,N+1) cout << val[i] << ' ' << mxVal[i] << endl;*/
    	while(sz(adj[b])>1){
    		int mx=-1,nxt;
    		for(auto v: adj[b]) 
    			if(v.fi!=pb && !taken[v.fi] && ckmax(mx,mxVal[v.fi]+v.se)) 
    				nxt=v.fi;
			if(mx==-1) break;
    		for(auto &v: adj[b]) if(v.fi==nxt) v.se=0;
    		for(auto &v: adj[nxt]) if(v.fi==b) v.se=0;
    		pb=b; b=nxt; 
    	}

    	ans++;
    	res.pb({a,b});
    	/*dbg(u);
    	dbgs(a,b);*/
    }

    cout << ans << endl;
    for(auto x: res) cout << x.fi << ' ' << x.se << endl;
    

    return 0;
}

/* Careful!!!
    .Array bounds
    .Infinite loops
    .Uninitialized variables / empty containers
    .Multisets are shit

   Some insights:
    .Binary search
    .Graph representation
    .Write brute force code
    .Change your approach
*/

Compilation message

net.cpp: In function 'int32_t main()':
net.cpp:130:7: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  130 |       for(auto v: adj[b])
      |       ^~~
net.cpp:133:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  133 |    if(mx==-1) break;
      |    ^~
net.cpp: At global scope:
net.cpp:194:11: error: redefinition of 'const ll MOD'
  194 | const int MOD = 1e9 + 7; //998244353
      |           ^~~
net.cpp:32:11: note: 'const ll MOD' previously defined here
   32 | const int MOD = 1e9 + 7; //998244353
      |           ^~~
net.cpp:195:10: error: redefinition of 'const ll INF'
  195 | const ll INF = 1e18;
      |          ^~~
net.cpp:33:10: note: 'const ll INF' previously defined here
   33 | const ll INF = 1e18;
      |          ^~~
net.cpp:196:11: error: redefinition of 'const ll MX'
  196 | const int MX = 2e5 + 10;
      |           ^~
net.cpp:34:11: note: 'const ll MX' previously defined here
   34 | const int MX = 2e5 + 10;
      |           ^~
net.cpp:197:11: error: redefinition of 'const ll nx [4]'
  197 | const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
      |           ^~
net.cpp:35:11: note: 'const ll nx [4]' previously defined here
   35 | const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
      |           ^~
net.cpp:197:34: error: redefinition of 'const ll ny [4]'
  197 | const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
      |                                  ^~
net.cpp:35:34: note: 'const ll ny [4]' previously defined here
   35 | const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
      |                                  ^~
net.cpp:200:24: error: redefinition of 'template<class T> bool ckmin(T&, const T&)'
  200 | template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
      |                        ^~~~~
net.cpp:38:24: note: 'template<class T> bool ckmin(T&, const T&)' previously declared here
   38 | template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
      |                        ^~~~~
net.cpp:201:24: error: redefinition of 'template<class T> bool ckmax(T&, const T&)'
  201 | template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
      |                        ^~~~~
net.cpp:39:24: note: 'template<class T> bool ckmax(T&, const T&)' previously declared here
   39 | template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
      |                        ^~~~~
net.cpp:203:4: error: redefinition of 'll cdiv(ll, ll)'
  203 | ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
      |    ^~~~
net.cpp:41:4: note: 'll cdiv(ll, ll)' previously defined here
   41 | ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
      |    ^~~~
net.cpp:206:9: error: redefinition of 'std::mt19937 rng'
  206 | mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
      |         ^~~
net.cpp:44:9: note: 'std::mt19937 rng' previously declared here
   44 | mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
      |         ^~~
net.cpp:209:4: error: redefinition of 'll random(ll, ll)'
  209 | ll random(ll a, ll b){
      |    ^~~~~~
net.cpp:47:4: note: 'll random(ll, ll)' previously defined here
   47 | ll random(ll a, ll b){
      |    ^~~~~~
net.cpp:221:6: error: redefinition of 'void IO()'
  221 | void IO() {
      |      ^~
net.cpp:59:6: note: 'void IO()' previously defined here
   59 | void IO() {
      |      ^~
net.cpp:228:5: error: redefinition of 'll N'
  228 | int N;
      |     ^
net.cpp:66:5: note: 'll N' previously declared here
   66 | int N;
      |     ^
net.cpp:229:5: error: redefinition of 'vpi adj [200010]'
  229 | vpi adj[MX];
      |     ^~~
net.cpp:67:5: note: 'vpi adj [200010]' previously declared here
   67 | vpi adj[MX];
      |     ^~~
net.cpp:231:6: error: redefinition of 'bool finished()'
  231 | bool finished(){
      |      ^~~~~~~~
net.cpp:69:6: note: 'bool finished()' previously defined here
   69 | bool finished(){
      |      ^~~~~~~~
net.cpp:236:5: error: redefinition of 'll val [200010]'
  236 | int val[MX],mxVal[MX];
      |     ^~~
net.cpp:74:5: note: 'll val [200010]' previously declared here
   74 | int val[MX],mxVal[MX];
      |     ^~~
net.cpp:236:13: error: redefinition of 'll mxVal [200010]'
  236 | int val[MX],mxVal[MX];
      |             ^~~~~
net.cpp:74:13: note: 'll mxVal [200010]' previously declared here
   74 | int val[MX],mxVal[MX];
      |             ^~~~~
net.cpp:237:6: error: redefinition of 'void dfs(ll, ll)'
  237 | void dfs(int u, int p){
      |      ^~~
net.cpp:75:6: note: 'void dfs(ll, ll)' previously defined here
   75 | void dfs(int u, int p){
      |      ^~~
net.cpp:252:9: error: redefinition of 'int32_t main()'
  252 | int32_t main() {
      |         ^~~~
net.cpp:90:9: note: 'int32_t main()' previously defined here
   90 | int32_t main() {
      |         ^~~~
net.cpp: In function 'int32_t main()':
net.cpp:292:7: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  292 |       for(auto v: adj[b])
      |       ^~~
net.cpp:295:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  295 |    if(mx==-1) break;
      |    ^~