【CF】135 Div2 Choosing Capital for Treeland

树形结构,挺有意思的题目。不难。

  1 /* 219D */
  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 = 2e5+5;
 43 int D[maxn];
 44 int W[maxn], sw;
 45 int W_[maxn];
 46 vpii E[maxn];
 47 vi vc;
 48 int ans;
 49 int n;
 50 
 51 void dfs(int u, int fa, int w_, int d) {
 52     int v, w;
 53     
 54     D[u] = d;
 55     W_[u] = w_;
 56     rep(i, 0, SZ(E[u])) {
 57         v = E[u][i].fir;
 58         w = E[u][i].sec;
 59         if (v == fa)
 60             continue;
 61         dfs(v, u, w_+w, d+1);
 62         W[u] += W[v] + w;
 63     }
 64 }
 65 
 66 void solve() {
 67     // calculate w & c
 68     dfs(1, -1, 0, 0);
 69     ans = sw = W[1];
 70     vc.pb(1);
 71     
 72     // calculate the minimum cost
 73     int tmp;
 74     
 75     rep(i, 2, n+1) {
 76         tmp = sw - W_[i] + D[i] - W_[i];
 77         if (tmp == ans) {
 78             vc.pb(i);
 79         } else if (tmp < ans) {
 80             vc.clear();
 81             ans = tmp;
 82             vc.pb(i);
 83         }
 84     }
 85     
 86     printf("%d\n", ans);
 87     rep(i, 0, SZ(vc))
 88         printf("%d ", vc[i]);
 89     putchar('\n');
 90 }
 91     
 92 int main() {
 93     ios::sync_with_stdio(false);
 94     #ifndef ONLINE_JUDGE
 95         freopen("data.in", "r", stdin);
 96         freopen("data.out", "w", stdout);
 97     #endif
 98     
 99     int u, v;
100     
101     scanf("%d", &n);
102     rep(i, 1, n) {
103         scanf("%d %d", &u, &v);
104         E[u].pb(mp(v, 0));
105         E[v].pb(mp(u, 1));
106     }
107     
108     solve();
109     
110     #ifndef ONLINE_JUDGE
111         printf("time = %d.\n", (int)clock());
112     #endif
113     
114     return 0;
115 }

 

posted on 2015-07-05 21:10  Bombe  阅读(257)  评论(0编辑  收藏  举报

导航