Submission #407583

# Submission time Handle Problem Language Result Execution time Memory
407583 2021-05-19T03:42:56 Z balbit Mergers (JOI19_mergers) C++14
70 / 100
191 ms 32192 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define y1 zck_is_king
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) 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...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif

const int iinf = 1e9+10;
const ll inf = 1ll<<60;
const ll mod = 1e9+7 ;


void GG(){cout<<"0\n"; exit(0);}

ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
    ll re=1;
    while (n>0){
        if (n&1) re = re*a %mo;
        a = a*a %mo;
        n>>=1;
    }
    return re;
}

ll inv (ll b, ll mo = mod){
    if (b==1) return b;
    return (mo-mo/b) * inv(mo%b,mo) % mo;
}

const int maxn = 1e5+5;

vector<int> g[maxn];
int dpar[maxn];
int par[maxn], dep[maxn];

void dfs(int v, int p) {
    par[v] = p;
    for (int u : g[v]) {
        if (u != p) {
            dep[u] = dep[v] + 1;
            dfs(u,v);
        }
    }
}

vector<int> where[maxn];
int find (int x ) {return x == dpar[x] ? x : dpar[x] = find(dpar[x]); }
void mrg(int a,int b ) {
    a = find(a); b = find(b);
    if (dep[a] > dep[b]) swap(a,b); // a is new root;
    dpar[b] = a;
}

set<int> adj[maxn];

signed main(){
    IOS();
    int n,k; cin>>n>>k;
    vector<pii> E;
    REP(i,n-1) {
        int a,b; cin>>a>>b; --a; --b;
        E.pb({a,b});
        g[a].pb(b); g[b].pb(a);
    }
    dfs(0, -1);
    REP(i,n) dpar[i ]= i;
    REP(i,n) {
        int x; cin>>x;
        where[x-1].pb(i);
    }
    REP(c, k) {
        int tmp = where[c][0];
        tmp = find(tmp);
        for (int i = 1; i<SZ(where[c]); ++i) {
            int v = where[c][i];
            v = find(v);
            while (v != tmp) {
                if (dep[v] < dep[tmp]) swap(v, tmp);
                // move v
                mrg(v, par[v]);
                v = find(v);
                bug(v, tmp);
            }
        }
    }
    for (pii p : E) {
        if(find (p.f) != find(p.s)) {
            adj[find(p.f)].insert(find(p.s));
            adj[find(p.s)].insert(find(p.f));
        }
    }
    int lf = 0;
    REP(i,n) {
        if (find (i) == i) {
            if (SZ(adj[i]) == 1) lf++;
        }
    }
    assert(lf != 1);
    cout<<(lf+1)/2<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 7 ms 9688 KB Output is correct
5 Correct 7 ms 9804 KB Output is correct
6 Correct 7 ms 9720 KB Output is correct
7 Correct 6 ms 9724 KB Output is correct
8 Correct 7 ms 9676 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 7 ms 9720 KB Output is correct
12 Correct 7 ms 9724 KB Output is correct
13 Correct 7 ms 9676 KB Output is correct
14 Correct 7 ms 9716 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9676 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 7 ms 9676 KB Output is correct
20 Correct 8 ms 9716 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
22 Correct 7 ms 9676 KB Output is correct
23 Correct 7 ms 9676 KB Output is correct
24 Correct 7 ms 9720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 7 ms 9688 KB Output is correct
5 Correct 7 ms 9804 KB Output is correct
6 Correct 7 ms 9720 KB Output is correct
7 Correct 6 ms 9724 KB Output is correct
8 Correct 7 ms 9676 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 7 ms 9720 KB Output is correct
12 Correct 7 ms 9724 KB Output is correct
13 Correct 7 ms 9676 KB Output is correct
14 Correct 7 ms 9716 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9676 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 7 ms 9676 KB Output is correct
20 Correct 8 ms 9716 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
22 Correct 7 ms 9676 KB Output is correct
23 Correct 7 ms 9676 KB Output is correct
24 Correct 7 ms 9720 KB Output is correct
25 Correct 7 ms 9728 KB Output is correct
26 Correct 10 ms 10124 KB Output is correct
27 Correct 8 ms 9848 KB Output is correct
28 Correct 9 ms 10188 KB Output is correct
29 Correct 10 ms 10188 KB Output is correct
30 Correct 8 ms 9844 KB Output is correct
31 Correct 7 ms 9676 KB Output is correct
32 Correct 9 ms 10188 KB Output is correct
33 Correct 9 ms 9676 KB Output is correct
34 Correct 11 ms 9932 KB Output is correct
35 Correct 12 ms 10244 KB Output is correct
36 Correct 10 ms 9912 KB Output is correct
37 Correct 9 ms 9932 KB Output is correct
38 Correct 7 ms 9716 KB Output is correct
39 Correct 9 ms 9932 KB Output is correct
40 Correct 8 ms 9932 KB Output is correct
41 Correct 8 ms 9932 KB Output is correct
42 Correct 9 ms 9948 KB Output is correct
43 Correct 9 ms 10072 KB Output is correct
44 Correct 7 ms 9676 KB Output is correct
45 Correct 11 ms 10032 KB Output is correct
46 Correct 9 ms 9932 KB Output is correct
47 Correct 7 ms 9676 KB Output is correct
48 Correct 9 ms 9932 KB Output is correct
49 Correct 9 ms 10316 KB Output is correct
50 Correct 11 ms 10316 KB Output is correct
51 Correct 8 ms 9932 KB Output is correct
52 Correct 8 ms 9860 KB Output is correct
53 Correct 9 ms 9992 KB Output is correct
54 Correct 10 ms 9904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 7 ms 9688 KB Output is correct
5 Correct 7 ms 9804 KB Output is correct
6 Correct 7 ms 9720 KB Output is correct
7 Correct 6 ms 9724 KB Output is correct
8 Correct 7 ms 9676 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 7 ms 9720 KB Output is correct
12 Correct 7 ms 9724 KB Output is correct
13 Correct 7 ms 9676 KB Output is correct
14 Correct 7 ms 9716 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9676 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 7 ms 9676 KB Output is correct
20 Correct 8 ms 9716 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
22 Correct 7 ms 9676 KB Output is correct
23 Correct 7 ms 9676 KB Output is correct
24 Correct 7 ms 9720 KB Output is correct
25 Correct 7 ms 9676 KB Output is correct
26 Correct 60 ms 17104 KB Output is correct
27 Correct 76 ms 16848 KB Output is correct
28 Correct 9 ms 9932 KB Output is correct
29 Correct 8 ms 9724 KB Output is correct
30 Correct 7 ms 9676 KB Output is correct
31 Correct 75 ms 16792 KB Output is correct
32 Correct 9 ms 9932 KB Output is correct
33 Correct 116 ms 22496 KB Output is correct
34 Correct 104 ms 16808 KB Output is correct
35 Correct 9 ms 9864 KB Output is correct
36 Correct 77 ms 17296 KB Output is correct
37 Correct 9 ms 9932 KB Output is correct
38 Correct 8 ms 9932 KB Output is correct
39 Correct 59 ms 17112 KB Output is correct
40 Correct 8 ms 10072 KB Output is correct
41 Correct 77 ms 17092 KB Output is correct
42 Correct 98 ms 18672 KB Output is correct
43 Correct 7 ms 9676 KB Output is correct
44 Correct 85 ms 23064 KB Output is correct
45 Correct 78 ms 19764 KB Output is correct
46 Correct 8 ms 10060 KB Output is correct
47 Correct 8 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 17156 KB Output is correct
2 Correct 191 ms 29380 KB Output is correct
3 Correct 9 ms 10088 KB Output is correct
4 Correct 9 ms 9932 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 7 ms 9676 KB Output is correct
7 Correct 9 ms 9932 KB Output is correct
8 Correct 94 ms 19216 KB Output is correct
9 Correct 8 ms 9932 KB Output is correct
10 Correct 88 ms 16976 KB Output is correct
11 Correct 8 ms 9676 KB Output is correct
12 Correct 84 ms 16872 KB Output is correct
13 Correct 92 ms 19896 KB Output is correct
14 Correct 164 ms 29132 KB Output is correct
15 Correct 75 ms 17056 KB Output is correct
16 Correct 9 ms 9980 KB Output is correct
17 Correct 7 ms 9712 KB Output is correct
18 Correct 170 ms 27236 KB Output is correct
19 Correct 138 ms 32192 KB Output is correct
20 Correct 10 ms 9932 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
22 Correct 100 ms 20044 KB Output is correct
23 Correct 9 ms 9872 KB Output is correct
24 Correct 92 ms 18108 KB Output is correct
25 Correct 152 ms 31160 KB Output is correct
26 Correct 9 ms 10228 KB Output is correct
27 Correct 9 ms 10232 KB Output is correct
28 Correct 9 ms 9988 KB Output is correct
29 Correct 8 ms 9960 KB Output is correct
30 Correct 9 ms 9956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 7 ms 9688 KB Output is correct
5 Correct 7 ms 9804 KB Output is correct
6 Correct 7 ms 9720 KB Output is correct
7 Correct 6 ms 9724 KB Output is correct
8 Correct 7 ms 9676 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 7 ms 9720 KB Output is correct
12 Correct 7 ms 9724 KB Output is correct
13 Correct 7 ms 9676 KB Output is correct
14 Correct 7 ms 9716 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9676 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 7 ms 9676 KB Output is correct
20 Correct 8 ms 9716 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
22 Correct 7 ms 9676 KB Output is correct
23 Correct 7 ms 9676 KB Output is correct
24 Correct 7 ms 9720 KB Output is correct
25 Correct 7 ms 9728 KB Output is correct
26 Correct 10 ms 10124 KB Output is correct
27 Correct 8 ms 9848 KB Output is correct
28 Correct 9 ms 10188 KB Output is correct
29 Correct 10 ms 10188 KB Output is correct
30 Correct 8 ms 9844 KB Output is correct
31 Correct 7 ms 9676 KB Output is correct
32 Correct 9 ms 10188 KB Output is correct
33 Correct 9 ms 9676 KB Output is correct
34 Correct 11 ms 9932 KB Output is correct
35 Correct 12 ms 10244 KB Output is correct
36 Correct 10 ms 9912 KB Output is correct
37 Correct 9 ms 9932 KB Output is correct
38 Correct 7 ms 9716 KB Output is correct
39 Correct 9 ms 9932 KB Output is correct
40 Correct 8 ms 9932 KB Output is correct
41 Correct 8 ms 9932 KB Output is correct
42 Correct 9 ms 9948 KB Output is correct
43 Correct 9 ms 10072 KB Output is correct
44 Correct 7 ms 9676 KB Output is correct
45 Correct 11 ms 10032 KB Output is correct
46 Correct 9 ms 9932 KB Output is correct
47 Correct 7 ms 9676 KB Output is correct
48 Correct 9 ms 9932 KB Output is correct
49 Correct 9 ms 10316 KB Output is correct
50 Correct 11 ms 10316 KB Output is correct
51 Correct 8 ms 9932 KB Output is correct
52 Correct 8 ms 9860 KB Output is correct
53 Correct 9 ms 9992 KB Output is correct
54 Correct 10 ms 9904 KB Output is correct
55 Correct 7 ms 9676 KB Output is correct
56 Correct 60 ms 17104 KB Output is correct
57 Correct 76 ms 16848 KB Output is correct
58 Correct 9 ms 9932 KB Output is correct
59 Correct 8 ms 9724 KB Output is correct
60 Correct 7 ms 9676 KB Output is correct
61 Correct 75 ms 16792 KB Output is correct
62 Correct 9 ms 9932 KB Output is correct
63 Correct 116 ms 22496 KB Output is correct
64 Correct 104 ms 16808 KB Output is correct
65 Correct 9 ms 9864 KB Output is correct
66 Correct 77 ms 17296 KB Output is correct
67 Correct 9 ms 9932 KB Output is correct
68 Correct 8 ms 9932 KB Output is correct
69 Correct 59 ms 17112 KB Output is correct
70 Correct 8 ms 10072 KB Output is correct
71 Correct 77 ms 17092 KB Output is correct
72 Correct 98 ms 18672 KB Output is correct
73 Correct 7 ms 9676 KB Output is correct
74 Correct 85 ms 23064 KB Output is correct
75 Correct 78 ms 19764 KB Output is correct
76 Correct 8 ms 10060 KB Output is correct
77 Correct 8 ms 9856 KB Output is correct
78 Correct 60 ms 17156 KB Output is correct
79 Correct 191 ms 29380 KB Output is correct
80 Correct 9 ms 10088 KB Output is correct
81 Correct 9 ms 9932 KB Output is correct
82 Correct 7 ms 9676 KB Output is correct
83 Correct 7 ms 9676 KB Output is correct
84 Correct 9 ms 9932 KB Output is correct
85 Correct 94 ms 19216 KB Output is correct
86 Correct 8 ms 9932 KB Output is correct
87 Correct 88 ms 16976 KB Output is correct
88 Correct 8 ms 9676 KB Output is correct
89 Correct 84 ms 16872 KB Output is correct
90 Correct 92 ms 19896 KB Output is correct
91 Correct 164 ms 29132 KB Output is correct
92 Correct 75 ms 17056 KB Output is correct
93 Correct 9 ms 9980 KB Output is correct
94 Correct 7 ms 9712 KB Output is correct
95 Correct 170 ms 27236 KB Output is correct
96 Correct 138 ms 32192 KB Output is correct
97 Correct 10 ms 9932 KB Output is correct
98 Correct 7 ms 9676 KB Output is correct
99 Correct 100 ms 20044 KB Output is correct
100 Correct 9 ms 9872 KB Output is correct
101 Correct 92 ms 18108 KB Output is correct
102 Correct 152 ms 31160 KB Output is correct
103 Correct 9 ms 10228 KB Output is correct
104 Correct 9 ms 10232 KB Output is correct
105 Correct 9 ms 9988 KB Output is correct
106 Correct 8 ms 9960 KB Output is correct
107 Correct 9 ms 9956 KB Output is correct
108 Runtime error 16 ms 19460 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -