hdu 3938 Portal离线并查集

题意搞得迷迷糊糊的

参考:http://blog.csdn.net/sdj222555/article/details/7439187

  1 #include<iostream>

  2 #include<cstdio>

  3 #include<cstdlib>

  4 #include<cstring>

  5 #include<string>

  6 #include<queue>

  7 #include<algorithm>

  8 #include<map>

  9 #include<iomanip>

 10 #define INF 99999999

 11 #define MAXN 10005

 12 using namespace std;

 13 

 14 int fa[MAXN], num[MAXN];

 15 long long out[MAXN];

 16 int n, m, q;

 17 

 18 //适用于正负整数

 19 template <class T>

 20 inline bool scan_d(T &ret) {

 21     char c; int sgn;

 22     if (c = getchar(), c == EOF) return 0; //EOF

 23     while (c != '-' && (c<'0' || c>'9')) c = getchar();

 24     sgn = (c == '-') ? -1 : 1;

 25     ret = (c == '-') ? 0 : (c - '0');

 26     while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');

 27     ret *= sgn;

 28     return 1;

 29 }

 30 

 31 struct node {

 32     int u, v, w;

 33     bool operator < (const node& a) const {

 34         return w < a.w;

 35     }

 36 }edge[MAXN*10];

 37 

 38 struct wen {

 39     int l, id;

 40     bool operator <(const wen &a) const {

 41         return l < a.l;

 42     }

 43 }p[MAXN];

 44 

 45 void init()

 46 {

 47     for (int i = 1; i <= n; ++i) {

 48         fa[i] = i;

 49         num[i] = 1;

 50     }

 51 }

 52 

 53 int find(int x)

 54 {

 55     if (fa[x] == x) return x;

 56     int t = find(fa[x]);

 57     fa[x] = t;

 58     return t;

 59 }

 60 

 61 int join(int x, int y)

 62 {

 63     int fx = find(x);

 64     int fy = find(y);

 65     if (fx == fy) return 0;

 66     int t = num[fx] * num[fy];

 67     num[fx] += num[fy];

 68     num[fy] = 0; 

 69     fa[fy] = fx;

 70     return t;

 71 }

 72 

 73 int main()

 74 {

 75     while (cin >> n >> m >> q) {

 76         init(); 

 77         for (int i = 1; i <= m; ++i){

 78             //cin >> edge[i].u >> edge[i].v >> edge[i].w;

 79             scan_d(edge[i].u);

 80             scan_d(edge[i].v);

 81             scan_d(edge[i].w);

 82         }

 83         sort(edge + 1, edge + m + 1);

 84         for (int i = 1; i <= q; ++i) {

 85             //cin >> p[i].l;

 86             scan_d(p[i].l);

 87             p[i].id = i;

 88         }

 89         sort(p+1,p+q+1);

 90         int pos = 1;

 91         long long ans = 0;

 92         for (int i = 1; i <= q; ++i) {

 93             while (pos <= m && edge[pos].w <= p[i].l) {

 94                 ans += join(edge[pos].u,edge[pos].v);

 95                 pos++;

 96             }

 97             out[p[i].id] = ans;

 98         }

 99         for (int i = 1; i <= q; ++i) {

100             cout << out[i] << endl;

101         }

102     }

103     return 0;

104 }
代码搬运工

 

你可能感兴趣的:(Portal)