Submission #341435

# Submission time Handle Problem Language Result Execution time Memory
341435 2020-12-29T17:07:26 Z amunduzbaev Shymbulak (IZhO14_shymbulak) C++14
50 / 100
1500 ms 12016 KB
/** made by amunduzbaev **/
 
#include <bits/stdc++.h>
using namespace std;
 
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vll vector<ll>
#define vii vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define cnt(a)__builtin_popcount(a)
template<class T> bool umin(T& a, const T& b) {return a > b? a = b, true:false;}
template<class T> bool umax(T& a, const T& b) {return a < b? a = b, true:false;}
 
const int N = 2e5+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
int s, n, m, k;
pii ans;

vii edges[N];
stack<int> ss;
bool f, used[N], cyc[N];
pii mxd[N];
vii cc;

void dfs(int u, int p){
	if(!cc.empty()) return;
	if(used[u]){
		while(!ss.empty() && ss.top() != u){
			cc.pb(ss.top());
			ss.pop();
		}f = 1;
		cc.pb(u);return;
	}
	used[u] = 1;
	ss.push(u);
	for(auto x:edges[u]) if(x != p) dfs(x, u);
	if(!ss.empty()) ss.pop();
}

pii merge(pii a, pii b){
	if(a.ff > b.ff) return a;
	if(b.ff > a.ff) return b;
	return mp(a.ff, a.ss + b.ss);
}
pii find_tree(int u, int p){
	vpii tmp;
	for(auto x:edges[u]){
		if(cyc[x] || x == p) continue;
		pii tt = find_tree(x, u);
		tmp.pb(tt);
		mxd[u] = merge(tt, mxd[u]);
	}
	sort(all(tmp));
	int n = sz(tmp);
	if(n >= 2){
		pii b = tmp[n-2], a = tmp[n-1], c = {a.ff + b.ff, 0};
		//cout<<a.ff<<" "<<a.ss<<endl;
		//cout<<b.ff<<" "<<b.ss<<endl;
		for(int i=0;i<n;i++){
			if(tmp[i].ff == b.ff) c.ss += tmp[i].ss;
		}
		if(a.ff != b.ff){
			c.ss *= a.ss;
			ans = merge(ans, c);
		}else{
			c.ss *= c.ss;
			for(auto x:tmp){
				if(x.ff == b.ff) c.ss -= x.ss * x.ss;
			}
			c.ss /= 2;
			//cout<<c.ss<<" "<<c.ff<<endl;
			ans = merge(c, ans);
		}
	}else { ans = merge(ans, mxd[u]); }
	return { mxd[u].ff+1, mxd[u].ss}; //, mxd[u].ss++;
}

void solve(){
	cin>>n;
	
	for(int i=0;i<n;i++){
		int a, b;
		cin>>a>>b;
		edges[a].pb(b);
		edges[b].pb(a);
	}
	
	for(int i=1;i<=n;i++){
		mxd[i] = {0, 1};
	}
	
	dfs(1, -1);
	int cyclen = sz(cc);
	for(auto x:cc) cyc[x] = 1;
	
	for(int i=1;i<=n;i++){
		if(cyc[i]){
			find_tree(i, -1);
		}
	}
	
	//for(auto x:cc) cout<<x<<" ";
	//cout<<"\n";
	//for(auto x:cc) cout<<mxd[x].ff<<" "<<mxd[x].ss<<"\n";
	//cout<<"\n";	
	
	for(int i=0;i<cyclen;i++){
		for(int j=i+1;j<cyclen;j++){
			int dis = min(j-i, cyclen - (j-i));
			pii tmp = {mxd[cc[i]].ff + mxd[cc[j]].ff + dis, mxd[cc[i]].ss * mxd[cc[j]].ss};
			if(j-i == cyclen - (j-i)) tmp.ss *= 2;
			ans = merge(ans, tmp);
		}
	}
	//cout<<ans.ff<<" "<<ans.ss; //ff<<" "<<ans.ss<<endl;
	cout<<ans.ss<<"\n";
	return;
}

/*

13
1 2
2 3
3 4
4 5
5 1
5 12
5 13
12 11
12 10
2 6
6 7
3 8
3 9

*/

int main(){
	fastios
	int t = 0;
	if(!t) solve();
	else {
		cin>>t;
		while (t--) solve();
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 6 ms 5248 KB Output is correct
6 Correct 5 ms 5356 KB Output is correct
7 Correct 6 ms 5228 KB Output is correct
8 Correct 6 ms 5228 KB Output is correct
9 Correct 5 ms 5228 KB Output is correct
10 Correct 5 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 8812 KB Output is correct
2 Correct 67 ms 9324 KB Output is correct
3 Execution timed out 1577 ms 12016 KB Time limit exceeded
4 Halted 0 ms 0 KB -