답안 #341600

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341600 2020-12-30T07:32:29 Z amunduzbaev 관광지 (IZhO14_shymbulak) C++14
50 / 100
172 ms 15584 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);
		}
	}
	
	
	int p=0;
	map<int, int> mm;
	
	
	for(int i=0;i<cyclen-1;i++){
		while(p < cyclen && p+p <= cyclen+i+i){
			mm[(mxd[cc[p]].ff+p)] += mxd[cc[p]].ss;
			p++;
		}
		mm[mxd[cc[i]].ff+i] -= mxd[cc[i]].ss;
		if(!mm[mxd[cc[i]].ff+i]) mm.erase(mxd[cc[i]].ff+i);
		pii tmp = *mm.rbegin();
		ans = merge(ans, {mxd[cc[i]].ff+tmp.ff-i, tmp.ss*mxd[cc[i]].ss});
	}
	
	mm.clear();
	p = cyclen-1;
	
	for(int i=cyclen-1;i>=0;i--){
		while(p+p >= cyclen+i+i){
			mm[mxd[cc[p]].ff+cyclen-p] += mxd[cc[p]].ss;
			p--;
		}
		if(mm.size()){
			pii tmp = *mm.rbegin();
			ans = merge(ans, {mxd[cc[i]].ff+i+tmp.ff, tmp.ss*mxd[cc[i]].ss});
		}
		//cout<<i<<" "<<ans.ff<<" "<<ans.ss<<"\n___\n";
	}
	//cout<<ans.ff<<" "<<ans.ss<<"\n";
	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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 3 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 5 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 3 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 5 ms 5228 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 5248 KB Output is correct
10 Correct 5 ms 5228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 8812 KB Output is correct
2 Correct 73 ms 9324 KB Output is correct
3 Correct 57 ms 12528 KB Output is correct
4 Correct 43 ms 9248 KB Output is correct
5 Correct 44 ms 9324 KB Output is correct
6 Correct 172 ms 12268 KB Output is correct
7 Correct 119 ms 10988 KB Output is correct
8 Correct 85 ms 13440 KB Output is correct
9 Correct 105 ms 13420 KB Output is correct
10 Correct 78 ms 14080 KB Output is correct
11 Incorrect 107 ms 15584 KB Output isn't correct
12 Halted 0 ms 0 KB -