답안 #341425

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341425 2020-12-29T16:22:14 Z amunduzbaev 관광지 (IZhO14_shymbulak) C++14
0 / 100
73 ms 9324 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);
}

void find_tree(int u, int p){
	vpii tmp;
	for(auto x:edges[u]){
		if(cyc[x] || x == p) continue;
		find_tree(x, u);
		tmp.pb(mxd[x]);
		mxd[u] = merge(mxd[x], 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};
		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;
			ans = merge(c, ans);
		}
	}else { ans = merge(ans, mxd[u]); }
	mxd[u].ff++; //, 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);
			//mxd[i].ff--;
		}
	}
	
	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.ss; //ff<<" "<<ans.ss<<endl;
	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 Incorrect 4 ms 5100 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Incorrect 4 ms 5100 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 8812 KB Output is correct
2 Incorrect 73 ms 9324 KB Output isn't correct
3 Halted 0 ms 0 KB -