답안 #337815

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
337815 2020-12-21T18:25:06 Z Kerim 관광지 (IZhO14_shymbulak) C++17
50 / 100
10 ms 6252 KB
#include "bits/stdc++.h"
#define MAXN 200009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;
 
typedef long long ll;
typedef pair<int,ll> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
stack<int>st;
vector<int>adj[MAXN],va;
int vis[MAXN];
void dfs1(int nd,int pr){
	if(!va.empty())
		return;
	if(vis[nd]){
		while(!st.empty() and st.top()!=nd)
			va.pb(st.top()),st.pop();
		va.pb(nd);
		return;
	}
	st.push(nd);
	vis[nd]=1;
	tr(it,adj[nd])
		if(*it!=pr)
			dfs1(*it,nd);
	st.pop();
}
PII dp[MAXN],ans;
PII merge(PII x,PII y){
	if(x.ff>y.ff)
		return x;
	if(y.ff>x.ff)
		return y;
	return mp(x.ff,x.ss+y.ss);
}
void dfs(int nd,int pr){
	dp[nd]=mp(0,1LL);
	vector<PII>v;
	tr(it,adj[nd])
		if(*it!=pr and !vis[*it]){
			dfs(*it,nd);
			v.pb(dp[*it]);
			dp[nd]=merge(dp[nd],dp[*it]);
		}
	sort(all(v));
	if(int(v.size())>=2){
		PII b=v[(int)v.size()-2],a=v[(int)v.size()-1];
		PII c=mp(a.ff+b.ff,0LL);
		for(int i=0;i<int(v.size());i++)
			if(v[i].ff==b.ff)
				c.ss+=v[i].ss;
		if(a.ff!=b.ff){
			c.ss*=a.ss;
			ans=merge(ans,c);
		}	
		else{
			c.ss*=c.ss;
			for(int i=0;i<int(v.size());i++)
				if(v[i].ff==b.ff)
					c.ss-=v[i].ss*v[i].ss;
			c.ss/=2;
			ans=merge(ans,c);
		}
	}
	else
		ans=merge(ans,dp[nd]);
	dp[nd].ff++;
}
void son(){
	puts("0");
	exit(0);
}
map<int,ll>a;
int main(){
	int n;cin>>n;
	if(n>=90000)son();
	for(int i=1;i<=n;i++){
		int u,v;cin>>u>>v;
		if(u==v)son();
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs1(1,-1);int sz=int(va.size());
	memset(vis,0,sizeof vis);
	tr(it,va)
		vis[*it]=1;
	vector<PII>g;
	tr(it,va)
		dfs(*it,-1),g.pb(mp(dp[*it].ff-1,dp[*it].ss));
	/*
	int p=0;
	for(int i=0;i<sz-1;i++){
		while(p<sz and p+p<=sz+i+i)
			a[g[p].ff+p]+=g[p].ss,p++;
		a[g[i].ff+i]-=g[i].ss;
		if(!a[g[i].ff+i])
			a.erase(g[i].ff+i);
		PII tmp=*a.rbegin();
		ans=merge(ans,mp(tmp.ff+g[i].ff-i,g[i].ss*tmp.ss));
	}p=sz-1;a.clear();
	for(int i=sz-1;i>=0;i--){
		while(p+p>=sz+i+i)
			a[g[p].ff+sz-p]+=g[p].ss,p--;	
		if(a.size()){
			PII tmp=*a.rbegin();
			ans=merge(ans,mp(tmp.ff+g[i].ff+i,g[i].ss*tmp.ss));	
		}
	}*/
	for(int i=0;i<sz;i++)
		for(int j=i+1;j<sz;j++){
			if(j-i<=sz-j+i)
				ans=merge(ans,mp(g[i].ff+g[j].ff+j-i,g[i].ss*g[j].ss));
			if(j-i>=sz-j+i)
				ans=merge(ans,mp(g[i].ff+g[j].ff+sz-j+i,g[i].ss*g[j].ss));
		}
	cout<<ans.ss<<endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5740 KB Output is correct
2 Correct 4 ms 5740 KB Output is correct
3 Correct 5 ms 5740 KB Output is correct
4 Correct 4 ms 5740 KB Output is correct
5 Correct 4 ms 5740 KB Output is correct
6 Correct 4 ms 5740 KB Output is correct
7 Correct 4 ms 5740 KB Output is correct
8 Correct 4 ms 5740 KB Output is correct
9 Correct 4 ms 5740 KB Output is correct
10 Correct 4 ms 5740 KB Output is correct
11 Correct 5 ms 5740 KB Output is correct
12 Correct 4 ms 5740 KB Output is correct
13 Correct 4 ms 5868 KB Output is correct
14 Correct 4 ms 5868 KB Output is correct
15 Correct 4 ms 5868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5868 KB Output is correct
2 Correct 4 ms 5868 KB Output is correct
3 Correct 5 ms 5868 KB Output is correct
4 Correct 4 ms 5868 KB Output is correct
5 Correct 10 ms 5996 KB Output is correct
6 Correct 8 ms 6252 KB Output is correct
7 Correct 8 ms 5996 KB Output is correct
8 Correct 8 ms 5996 KB Output is correct
9 Correct 7 ms 5996 KB Output is correct
10 Correct 7 ms 5996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4972 KB Output isn't correct
2 Halted 0 ms 0 KB -