답안 #210303

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
210303 2020-03-17T02:47:34 Z ToMmyDong Mergers (JOI19_mergers) C++11
70 / 100
1826 ms 262148 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define REP(i,n) for(int i=0;i<n;++i)
#define REP1(i,n) for(int i=1;i<=n;++i)
#define SZ(i) int(i.size())
#define eb emplace_back
#define ALL(i) i.begin(),i.end()
#define X first
#define Y second
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"#%d: %s = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
template<typename It> ostream& _printRng(ostream &os,It bg,It ed)
{
    os<<"{";
    for(It it=bg;it!=ed;it++) {
        os<<(it==bg?"":",")<<*it;
    }
    os<<"}";
    return os;
}
template<typename T> ostream &operator << (ostream &os,vector<T> &v){return _printRng(os,v.begin(), v.end());}
template<typename T> void pary(T bg, T ed){_printRng(cerr,bg,ed);cerr<<endl;}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#define pary(...)
#endif

const int MAXN = 500005;
const ll MOD = 1000000007;

int n, k;
vector<pii> edges;
vector<int> G[MAXN], T[MAXN];

vector<int> tree, comp;

int s[MAXN], cnt[MAXN];
map<int,int> cur[MAXN];
set<int> open[MAXN];

void add (int f, int t) { //
    if (cur[f].size() > cur[t].size()) {
        swap(cur[f], cur[t]);
        swap(open[f], open[t]);
    }

    for (auto v : open[f]) {
        open[t].insert(v);
    }
    for (auto p : cur[f]) {
        cur[t][p.X] += p.Y;
        if (cur[t][p.X] == cnt[p.X]) {
            open[t].erase(p.X);
        }
    }
}

void dfsEdg(int nd, int pa, int id) {

    cur[nd][s[nd]]++;
    if (cnt[s[nd]] != 1) {
        open[nd].insert(s[nd]);
    }

    for (auto eid : G[nd]) {
        int v = edges[eid].X ^ edges[eid].Y ^ nd;
        if (v != pa) {
            dfsEdg(v, nd, eid);
            add(v, nd);
        }
    }

    if (id == -1) {
        return;
    }

    if (open[nd].empty()) {
        tree.eb(id);
    } else {
        comp.eb(id);
    }
}

int djs[MAXN];

int fnd (int nd) {
    return nd == djs[nd] ? djs[nd] : djs[nd] = fnd(djs[nd]);
}

void mrg (int x, int y) {
    x = fnd(x);
    y = fnd(y);
    djs[x] = y;
}

int deg[MAXN];
/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> n >> k;
    REP (i, n-1) {
        int u,v;
        cin >> u >> v;
        edges.eb(u, v);
        G[u].eb(i);
        G[v].eb(i);
    }

    REP1 (i, n) {
        cin >> s[i];
        cnt[s[i]]++;
    }


    dfsEdg(1, 1, -1);

    for (int i=1; i<=n; i++) {
        djs[i] = i;
    }

    for (auto id : comp) {
        mrg(edges[id].X, edges[id].Y);
    }
    vector<int> nodes;
    for (int i=1; i<=n; i++) {
        if (fnd(i) == i) {
            nodes.eb(i);
        }
    }

    debug(tree, comp, nodes);
    for (auto id : tree) {
        int u, v;
        tie(u, v) = edges[id];

        deg[fnd(u)]++;
        deg[fnd(v)]++;
    }

    int ans = 0;
    for (auto v : nodes) {
        ans += (deg[v] == 1);
    }

    if (ans == 1) {
        cout << 0 << endl;
    } else {
        cout << (ans+1)/2 << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 70776 KB Output is correct
2 Correct 42 ms 70776 KB Output is correct
3 Correct 43 ms 70776 KB Output is correct
4 Correct 42 ms 70776 KB Output is correct
5 Correct 42 ms 70776 KB Output is correct
6 Correct 43 ms 70776 KB Output is correct
7 Correct 43 ms 70776 KB Output is correct
8 Correct 45 ms 70776 KB Output is correct
9 Correct 42 ms 70776 KB Output is correct
10 Correct 43 ms 70776 KB Output is correct
11 Correct 43 ms 70780 KB Output is correct
12 Correct 42 ms 70776 KB Output is correct
13 Correct 42 ms 70776 KB Output is correct
14 Correct 43 ms 70776 KB Output is correct
15 Correct 42 ms 70776 KB Output is correct
16 Correct 42 ms 70776 KB Output is correct
17 Correct 42 ms 70776 KB Output is correct
18 Correct 43 ms 70776 KB Output is correct
19 Correct 44 ms 70776 KB Output is correct
20 Correct 43 ms 70776 KB Output is correct
21 Correct 42 ms 70776 KB Output is correct
22 Correct 43 ms 70776 KB Output is correct
23 Correct 42 ms 70776 KB Output is correct
24 Correct 43 ms 70776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 70776 KB Output is correct
2 Correct 42 ms 70776 KB Output is correct
3 Correct 43 ms 70776 KB Output is correct
4 Correct 42 ms 70776 KB Output is correct
5 Correct 42 ms 70776 KB Output is correct
6 Correct 43 ms 70776 KB Output is correct
7 Correct 43 ms 70776 KB Output is correct
8 Correct 45 ms 70776 KB Output is correct
9 Correct 42 ms 70776 KB Output is correct
10 Correct 43 ms 70776 KB Output is correct
11 Correct 43 ms 70780 KB Output is correct
12 Correct 42 ms 70776 KB Output is correct
13 Correct 42 ms 70776 KB Output is correct
14 Correct 43 ms 70776 KB Output is correct
15 Correct 42 ms 70776 KB Output is correct
16 Correct 42 ms 70776 KB Output is correct
17 Correct 42 ms 70776 KB Output is correct
18 Correct 43 ms 70776 KB Output is correct
19 Correct 44 ms 70776 KB Output is correct
20 Correct 43 ms 70776 KB Output is correct
21 Correct 42 ms 70776 KB Output is correct
22 Correct 43 ms 70776 KB Output is correct
23 Correct 42 ms 70776 KB Output is correct
24 Correct 43 ms 70776 KB Output is correct
25 Correct 43 ms 70776 KB Output is correct
26 Correct 46 ms 71544 KB Output is correct
27 Correct 45 ms 71292 KB Output is correct
28 Correct 46 ms 71544 KB Output is correct
29 Correct 48 ms 71852 KB Output is correct
30 Correct 47 ms 71800 KB Output is correct
31 Correct 44 ms 70780 KB Output is correct
32 Correct 47 ms 71544 KB Output is correct
33 Correct 44 ms 70776 KB Output is correct
34 Correct 45 ms 71288 KB Output is correct
35 Correct 47 ms 71672 KB Output is correct
36 Correct 45 ms 71288 KB Output is correct
37 Correct 49 ms 71800 KB Output is correct
38 Correct 43 ms 70776 KB Output is correct
39 Correct 49 ms 72056 KB Output is correct
40 Correct 46 ms 71288 KB Output is correct
41 Correct 45 ms 71288 KB Output is correct
42 Correct 47 ms 71416 KB Output is correct
43 Correct 44 ms 71544 KB Output is correct
44 Correct 43 ms 70776 KB Output is correct
45 Correct 48 ms 71544 KB Output is correct
46 Correct 48 ms 71672 KB Output is correct
47 Correct 43 ms 70776 KB Output is correct
48 Correct 46 ms 71416 KB Output is correct
49 Correct 45 ms 71416 KB Output is correct
50 Correct 46 ms 71544 KB Output is correct
51 Correct 48 ms 71416 KB Output is correct
52 Correct 45 ms 71288 KB Output is correct
53 Correct 46 ms 71416 KB Output is correct
54 Correct 49 ms 71672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 70776 KB Output is correct
2 Correct 42 ms 70776 KB Output is correct
3 Correct 43 ms 70776 KB Output is correct
4 Correct 42 ms 70776 KB Output is correct
5 Correct 42 ms 70776 KB Output is correct
6 Correct 43 ms 70776 KB Output is correct
7 Correct 43 ms 70776 KB Output is correct
8 Correct 45 ms 70776 KB Output is correct
9 Correct 42 ms 70776 KB Output is correct
10 Correct 43 ms 70776 KB Output is correct
11 Correct 43 ms 70780 KB Output is correct
12 Correct 42 ms 70776 KB Output is correct
13 Correct 42 ms 70776 KB Output is correct
14 Correct 43 ms 70776 KB Output is correct
15 Correct 42 ms 70776 KB Output is correct
16 Correct 42 ms 70776 KB Output is correct
17 Correct 42 ms 70776 KB Output is correct
18 Correct 43 ms 70776 KB Output is correct
19 Correct 44 ms 70776 KB Output is correct
20 Correct 43 ms 70776 KB Output is correct
21 Correct 42 ms 70776 KB Output is correct
22 Correct 43 ms 70776 KB Output is correct
23 Correct 42 ms 70776 KB Output is correct
24 Correct 43 ms 70776 KB Output is correct
25 Correct 45 ms 70776 KB Output is correct
26 Correct 177 ms 87320 KB Output is correct
27 Correct 309 ms 100204 KB Output is correct
28 Correct 46 ms 71288 KB Output is correct
29 Correct 44 ms 70776 KB Output is correct
30 Correct 44 ms 70776 KB Output is correct
31 Correct 191 ms 87016 KB Output is correct
32 Correct 44 ms 71288 KB Output is correct
33 Correct 239 ms 95976 KB Output is correct
34 Correct 281 ms 97516 KB Output is correct
35 Correct 45 ms 71288 KB Output is correct
36 Correct 223 ms 87912 KB Output is correct
37 Correct 46 ms 71288 KB Output is correct
38 Correct 44 ms 71292 KB Output is correct
39 Correct 174 ms 87404 KB Output is correct
40 Correct 48 ms 71544 KB Output is correct
41 Correct 204 ms 86764 KB Output is correct
42 Correct 233 ms 89704 KB Output is correct
43 Correct 43 ms 70776 KB Output is correct
44 Correct 218 ms 96280 KB Output is correct
45 Correct 247 ms 93672 KB Output is correct
46 Correct 46 ms 71288 KB Output is correct
47 Correct 46 ms 71672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 178 ms 85868 KB Output is correct
2 Correct 209 ms 87400 KB Output is correct
3 Correct 48 ms 71544 KB Output is correct
4 Correct 47 ms 71288 KB Output is correct
5 Correct 42 ms 70776 KB Output is correct
6 Correct 44 ms 70776 KB Output is correct
7 Correct 46 ms 71288 KB Output is correct
8 Correct 333 ms 103660 KB Output is correct
9 Correct 46 ms 71288 KB Output is correct
10 Correct 205 ms 87528 KB Output is correct
11 Correct 43 ms 70776 KB Output is correct
12 Correct 206 ms 87404 KB Output is correct
13 Correct 300 ms 99820 KB Output is correct
14 Correct 317 ms 108528 KB Output is correct
15 Correct 173 ms 87400 KB Output is correct
16 Correct 47 ms 71444 KB Output is correct
17 Correct 42 ms 70776 KB Output is correct
18 Correct 242 ms 90728 KB Output is correct
19 Correct 255 ms 95852 KB Output is correct
20 Correct 47 ms 71672 KB Output is correct
21 Correct 44 ms 70812 KB Output is correct
22 Correct 263 ms 90216 KB Output is correct
23 Correct 47 ms 71416 KB Output is correct
24 Correct 221 ms 89320 KB Output is correct
25 Correct 254 ms 94572 KB Output is correct
26 Correct 45 ms 71416 KB Output is correct
27 Correct 47 ms 71544 KB Output is correct
28 Correct 47 ms 71416 KB Output is correct
29 Correct 46 ms 71376 KB Output is correct
30 Correct 47 ms 71416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 70776 KB Output is correct
2 Correct 42 ms 70776 KB Output is correct
3 Correct 43 ms 70776 KB Output is correct
4 Correct 42 ms 70776 KB Output is correct
5 Correct 42 ms 70776 KB Output is correct
6 Correct 43 ms 70776 KB Output is correct
7 Correct 43 ms 70776 KB Output is correct
8 Correct 45 ms 70776 KB Output is correct
9 Correct 42 ms 70776 KB Output is correct
10 Correct 43 ms 70776 KB Output is correct
11 Correct 43 ms 70780 KB Output is correct
12 Correct 42 ms 70776 KB Output is correct
13 Correct 42 ms 70776 KB Output is correct
14 Correct 43 ms 70776 KB Output is correct
15 Correct 42 ms 70776 KB Output is correct
16 Correct 42 ms 70776 KB Output is correct
17 Correct 42 ms 70776 KB Output is correct
18 Correct 43 ms 70776 KB Output is correct
19 Correct 44 ms 70776 KB Output is correct
20 Correct 43 ms 70776 KB Output is correct
21 Correct 42 ms 70776 KB Output is correct
22 Correct 43 ms 70776 KB Output is correct
23 Correct 42 ms 70776 KB Output is correct
24 Correct 43 ms 70776 KB Output is correct
25 Correct 43 ms 70776 KB Output is correct
26 Correct 46 ms 71544 KB Output is correct
27 Correct 45 ms 71292 KB Output is correct
28 Correct 46 ms 71544 KB Output is correct
29 Correct 48 ms 71852 KB Output is correct
30 Correct 47 ms 71800 KB Output is correct
31 Correct 44 ms 70780 KB Output is correct
32 Correct 47 ms 71544 KB Output is correct
33 Correct 44 ms 70776 KB Output is correct
34 Correct 45 ms 71288 KB Output is correct
35 Correct 47 ms 71672 KB Output is correct
36 Correct 45 ms 71288 KB Output is correct
37 Correct 49 ms 71800 KB Output is correct
38 Correct 43 ms 70776 KB Output is correct
39 Correct 49 ms 72056 KB Output is correct
40 Correct 46 ms 71288 KB Output is correct
41 Correct 45 ms 71288 KB Output is correct
42 Correct 47 ms 71416 KB Output is correct
43 Correct 44 ms 71544 KB Output is correct
44 Correct 43 ms 70776 KB Output is correct
45 Correct 48 ms 71544 KB Output is correct
46 Correct 48 ms 71672 KB Output is correct
47 Correct 43 ms 70776 KB Output is correct
48 Correct 46 ms 71416 KB Output is correct
49 Correct 45 ms 71416 KB Output is correct
50 Correct 46 ms 71544 KB Output is correct
51 Correct 48 ms 71416 KB Output is correct
52 Correct 45 ms 71288 KB Output is correct
53 Correct 46 ms 71416 KB Output is correct
54 Correct 49 ms 71672 KB Output is correct
55 Correct 45 ms 70776 KB Output is correct
56 Correct 177 ms 87320 KB Output is correct
57 Correct 309 ms 100204 KB Output is correct
58 Correct 46 ms 71288 KB Output is correct
59 Correct 44 ms 70776 KB Output is correct
60 Correct 44 ms 70776 KB Output is correct
61 Correct 191 ms 87016 KB Output is correct
62 Correct 44 ms 71288 KB Output is correct
63 Correct 239 ms 95976 KB Output is correct
64 Correct 281 ms 97516 KB Output is correct
65 Correct 45 ms 71288 KB Output is correct
66 Correct 223 ms 87912 KB Output is correct
67 Correct 46 ms 71288 KB Output is correct
68 Correct 44 ms 71292 KB Output is correct
69 Correct 174 ms 87404 KB Output is correct
70 Correct 48 ms 71544 KB Output is correct
71 Correct 204 ms 86764 KB Output is correct
72 Correct 233 ms 89704 KB Output is correct
73 Correct 43 ms 70776 KB Output is correct
74 Correct 218 ms 96280 KB Output is correct
75 Correct 247 ms 93672 KB Output is correct
76 Correct 46 ms 71288 KB Output is correct
77 Correct 46 ms 71672 KB Output is correct
78 Correct 178 ms 85868 KB Output is correct
79 Correct 209 ms 87400 KB Output is correct
80 Correct 48 ms 71544 KB Output is correct
81 Correct 47 ms 71288 KB Output is correct
82 Correct 42 ms 70776 KB Output is correct
83 Correct 44 ms 70776 KB Output is correct
84 Correct 46 ms 71288 KB Output is correct
85 Correct 333 ms 103660 KB Output is correct
86 Correct 46 ms 71288 KB Output is correct
87 Correct 205 ms 87528 KB Output is correct
88 Correct 43 ms 70776 KB Output is correct
89 Correct 206 ms 87404 KB Output is correct
90 Correct 300 ms 99820 KB Output is correct
91 Correct 317 ms 108528 KB Output is correct
92 Correct 173 ms 87400 KB Output is correct
93 Correct 47 ms 71444 KB Output is correct
94 Correct 42 ms 70776 KB Output is correct
95 Correct 242 ms 90728 KB Output is correct
96 Correct 255 ms 95852 KB Output is correct
97 Correct 47 ms 71672 KB Output is correct
98 Correct 44 ms 70812 KB Output is correct
99 Correct 263 ms 90216 KB Output is correct
100 Correct 47 ms 71416 KB Output is correct
101 Correct 221 ms 89320 KB Output is correct
102 Correct 254 ms 94572 KB Output is correct
103 Correct 45 ms 71416 KB Output is correct
104 Correct 47 ms 71544 KB Output is correct
105 Correct 47 ms 71416 KB Output is correct
106 Correct 46 ms 71376 KB Output is correct
107 Correct 47 ms 71416 KB Output is correct
108 Correct 1735 ms 167004 KB Output is correct
109 Correct 1283 ms 173020 KB Output is correct
110 Correct 1348 ms 187996 KB Output is correct
111 Correct 1826 ms 198520 KB Output is correct
112 Correct 1718 ms 189532 KB Output is correct
113 Correct 1784 ms 165980 KB Output is correct
114 Correct 1173 ms 151392 KB Output is correct
115 Correct 1158 ms 151388 KB Output is correct
116 Runtime error 1432 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
117 Halted 0 ms 0 KB -