标签:
这种图论题已经变得简单了。。。
1 /* D */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 #include <iterator> 20 #include <iomanip> 21 using namespace std; 22 //#pragma comment(linker,"/STACK:102400000,1024000") 23 24 #define sti set<int> 25 #define stpii set<pair<int, int> > 26 #define mpii map<int,int> 27 #define vi vector<int> 28 #define pii pair<int,int> 29 #define vpii vector<pair<int,int> > 30 #define rep(i, a, n) for (int i=a;i<n;++i) 31 #define per(i, a, n) for (int i=n-1;i>=a;--i) 32 #define clr clear 33 #define pb push_back 34 #define mp make_pair 35 #define fir first 36 #define sec second 37 #define all(x) (x).begin(),(x).end() 38 #define SZ(x) ((int)(x).size()) 39 #define lson l, mid, rt<<1 40 #define rson mid+1, r, rt<<1|1 41 42 const int maxn = 123460; 43 vi vc[maxn], vc2[maxn]; 44 bool visit[maxn]; 45 bool mark[maxn]; 46 int markp[maxn], pre[maxn]; 47 int mx[maxn], mx_[maxn]; 48 int n, m, length = 0; 49 int mn = INT_MAX, ans = INT_MAX; 50 51 void bfs(int s) { 52 queue<int> Q; 53 int u, v, sz; 54 55 Q.push(s); 56 visit[s] = true; 57 pre[s] = 0; 58 59 while (!Q.empty()) { 60 u = Q.front(); 61 Q.pop(); 62 sz = SZ(vc[u]); 63 rep(i, 0, sz) { 64 v = vc[u][i]; 65 if (!visit[v]) { 66 visit[v] = true; 67 pre[v] = u; 68 Q.push(v); 69 } 70 } 71 } 72 } 73 74 void dfs3(int v) { 75 if (visit[v]) 76 return; 77 78 visit[v] = true; 79 int u = pre[v]; 80 81 length += 2; 82 vc2[u].pb(v); 83 vc2[v].pb(u); 84 if (u) 85 dfs3(u); 86 } 87 88 void dfs(int u, int fa) { 89 int v, sz = SZ(vc2[u]); 90 91 mx[u] = mx_[u] = 0; 92 rep(i, 0, sz) { 93 v = vc2[u][i]; 94 if (v == fa) 95 continue; 96 97 dfs(v, u); 98 if (mx[v]+1 > mx[u]) { 99 mx_[u] = mx[u]; 100 mx[u] = mx[v] + 1; 101 } else if (mx[v]+1 > mx_[u]) { 102 mx_[u] = mx[v]+1; 103 } 104 } 105 106 #ifndef ONLINE_JUDGE 107 printf("u = %d, mx[u] = %d, mx_[u] = %d\n", u, mx[u], mx_[u]); 108 #endif 109 } 110 111 void dfs2(int u, int fa, int pmx) { 112 int v, sz = SZ(vc2[u]); 113 int cmx = max(mx[u], pmx), tmp; 114 115 if (length-cmx < mn) { 116 mn = length - cmx; 117 ans = u; 118 } else if (length-cmx==mn && u<ans) { 119 ans = u; 120 } 121 122 rep(i, 0, sz) { 123 v = vc2[u][i]; 124 if (v == fa) 125 continue; 126 127 if (mx[v]+1 == mx[u]) { 128 tmp = max(pmx, mx_[u])+1; 129 } else { 130 tmp = max(pmx, mx[u])+1; 131 } 132 133 dfs2(v, u, tmp); 134 } 135 } 136 137 int main() { 138 ios::sync_with_stdio(false); 139 #ifndef ONLINE_JUDGE 140 freopen("data.in", "r", stdin); 141 freopen("data.out", "w", stdout); 142 #endif 143 144 int u, v, x; 145 146 scanf("%d %d", &n, &m); 147 rep(i, 1, n) { 148 scanf("%d %d", &u, &v); 149 vc[u].pb(v); 150 vc[v].pb(u); 151 } 152 153 rep(i, 0, m) { 154 scanf("%d", &x); 155 markp[i] = x; 156 mark[x] = true; 157 } 158 159 bfs(x); 160 memset(visit, false, sizeof(visit)); 161 visit[x] = true; 162 rep(i, 0, m) 163 dfs3(markp[i]); 164 dfs(x, 0); 165 dfs2(x, 0, 0); 166 167 printf("%d\n%d\n", ans, mn); 168 169 #ifndef ONLINE_JUDGE 170 printf("time = %d.\n", (int)clock()); 171 #endif 172 173 return 0; 174 }
标签:
原文地址:http://www.cnblogs.com/bombe1013/p/4926704.html