• cf1182D Complete Mirror


    • 可以得到一个结论, 可行的点要么是直径端点, 要么是直径中点, 要么是直径中点引出的链中最短的端点
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #define mmp make_pair
    #define ll long long
    #define M 100010
    using namespace std;
    int read() {
    	int nm = 0, f = 1;
    	char c = getchar();
    	for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
    	for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
    	return nm * f;
    }
    vector<int> to[M];
    int n;
    int sta[M], tp, a, b, mid, maxx;
    int du[M], deep[M], af[M];
    vector<int> note[M];
    
    void dfss(int now, int f) {
    	deep[now] = deep[f] + 1;
    	note[deep[now]].push_back(du[now]);
    	for(int i = 0; i < to[now].size(); i++) {
    		int vj = to[now][i];
    		if(vj == f) continue;
    		dfss(vj, now);
    	}
    }
    
    void dfs(int now, int f) {
    	sta[++tp] = now;
    	if(tp > maxx) {
    		maxx = tp;
    		b = now, mid = sta[(tp + 1) >> 1];
    	}
    	for(int i = 0; i < to[now].size(); i++) {
    		int vj = to[now][i];
    		if(vj == f) continue;
    		dfs(vj, now);
    	}
    	tp--;
    }
    
    void check(int x) {
    	for(int i = 1; i <= n; i++) vector<int>().swap(note[i]);
    	dfss(x, 0);
    	for(int i = 1; i <= n; i++) {
    		for(int j = 1; j < note[i].size(); j++) {
    			if(note[i][j] != note[i][j - 1]) return;
    		}
    	}
    	cout << x << "
    ";
    	exit(0);
    }
    
    void work(int now, int f) {
    	deep[now] = deep[f] + 1;
    	if(du[now] > 2) return;
    	if(du[now] == 1) {
    		if(maxx > deep[now]) {
    			maxx = deep[now], a = now;
    		}
    		return;
    	}
    	for(int i = 0; i < to[now].size(); i++) {
    		int vj = to[now][i];
    		if(vj == f) continue;
    		work(vj, now);
    	}
    }
    
    int main() {
    	n = read();
    	for(int i = 1; i < n; i++) {
    		int vi =  read(), vj = read();
    		to[vi].push_back(vj);
    		to[vj].push_back(vi);
    		du[vi]++;
    		du[vj]++;
    	}
    	a = 1;
    	dfs(1, 0);
    	maxx = 0;
    	a = b;
    	dfs(a, 0);
    	check(a);
    	check(b);
    	check(mid);
    	maxx = 0x3e3e3e3e;
    	for(int i = 0; i < to[mid].size(); i++) {
    		int vj = to[mid][i];
    		work(vj, mid);
    	}
    	check(a);
    	cout << "-1
    ";
    	return 0;
    }
    
  • 相关阅读:
    如何在main之后做一些事情
    Flash Component不显示[待解决]
    C++函数后面的throw()
    为甚马脚本中修改Text Sprite的Blend无效?
    sprite(channel).type的意义
    IDXGIDevice接口
    Director中调用AS3.0对象getVariable 附源码 20151228update
    MultiUser 03 – Serverside Script
    MultiUser02 使用Xtra和Server建立连接
    一部职场寓言——读《团队的五种机能障碍》
  • 原文地址:https://www.cnblogs.com/luoyibujue/p/11008651.html
Copyright © 2020-2023  润新知