标签:ssi alt 技术 more struct hose opened nim strong
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13865 | Accepted: 5880 |
Description
Input
Output
Sample Input
1 7 2 6 1 2 1 4 4 5 3 7 3 1
Sample Output
1 2
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 inline void read(int &x) 7 { 8 x = 0;char ch = getchar();char c = ch; 9 while(ch > ‘9‘ || ch < ‘0‘)c = ch, ch = getchar(); 10 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 11 if(c == ‘-‘)x = -x; 12 } 13 inline int max(int a, int b){return a > b ? a : b;} 14 inline int min(int a, int b){return a < b ? a : b;} 15 16 const int INF = 0x3f3f3f3f; 17 const int MAXN = 200000 + 10; 18 19 struct Edge 20 { 21 int u,v,next; 22 }edge[MAXN << 1]; 23 int t,n,head[MAXN],cnt,b[MAXN],dp[MAXN],ma,g; 24 inline void insert(int a, int b){edge[++cnt] = Edge{a,b,head[a]};head[a] = cnt;} 25 26 void dfs(int u) 27 { 28 int pos, tmp = -1; 29 dp[u] = 1; 30 for(pos = head[u];pos;pos = edge[pos].next) 31 { 32 int v = edge[pos].v; 33 if(!b[v]) 34 { 35 b[v] = true; 36 dfs(v); 37 dp[u] += dp[v]; 38 tmp = max(tmp, dp[v]); 39 } 40 } 41 tmp = max(tmp, n - dp[u]); 42 if(tmp < ma) 43 ma = tmp,g = u; 44 if(tmp == ma) 45 g = min(g, u); 46 } 47 48 int main() 49 { 50 read(t); 51 register int i,tmp1,tmp2; 52 for(;t;--t) 53 { 54 ma = INF,g = INF; 55 memset(edge, 0, sizeof(edge)); 56 memset(head, 0, sizeof(head)); 57 cnt = 0;memset(dp, 0, sizeof(dp)); 58 memset(b, 0, sizeof(b)); 59 read(n); 60 for(i = 1;i < n;++ i) 61 { 62 read(tmp1);read(tmp2); 63 insert(tmp1, tmp2);insert(tmp2, tmp1); 64 } 65 b[1] = true; 66 dfs(1); 67 printf("%d %d\n", g, ma); 68 } 69 return 0; 70 }
标签:ssi alt 技术 more struct hose opened nim strong
原文地址:http://www.cnblogs.com/huibixiaoxing/p/7123119.html