답안 #417599

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
417599 2021-06-04T02:24:11 Z cpp219 Mergers (JOI19_mergers) C++14
100 / 100
1912 ms 106832 KB
#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")

#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 5e5 + 9;
const ll inf = 1e9 + 7;
typedef pair<ll,ll> LL;
vector<ll> g[N],c[N];
ll n,k,x,y,in[N],out[N],nTime = 1,grp[N],cnt[N];
ll par[N];
void DFS(ll u,ll p){
    in[u] = out[u] = nTime; nTime++;
    par[u] = p;
    for (auto i : g[u])
        if (i != p) DFS(i,u),out[u] = max(out[u],out[i]);
}

ll st[2][4*N]; /// 0 for min   1 for max
void upd(ll cond,ll id,ll l,ll r,ll u,ll val){
    if (u < l||r < u) return;
    if (l == r){
        st[cond][id] = val; return;
    }
    ll mid = (l + r)/2;
    upd(cond,id*2,l,mid,u,val); upd(cond,id*2 + 1,mid + 1,r,u,val);
    if (!cond) st[cond][id] = min(st[cond][id*2],st[cond][id*2 + 1]);
    else st[cond][id] = max(st[cond][id*2],st[cond][id*2 + 1]);
}

ll Get(ll cond,ll id,ll l,ll r,ll u,ll v){
    if (v < l||r < u){
        if (!cond) return inf;
        return 0;
    }
    if (u <= l&&r <= v) return st[cond][id];
    ll mid = (l + r)/2;
    if (!cond) return min(Get(cond,id*2,l,mid,u,v),Get(cond,id*2 + 1,mid + 1,r,u,v));
    return  max(Get(cond,id*2,l,mid,u,v),Get(cond,id*2 + 1,mid + 1,r,u,v));
}
LL edge[N];
ll lab[N];
ll Find(ll u){
    if (lab[u] < 0) return u;
    return lab[u] = Find(lab[u]);
}

void Union(ll p,ll q){
    ll s = Find(p),r = Find(q);
    if (r == s) return;
    if (lab[r] > lab[s]) swap(r,s);
    lab[r] += lab[s]; lab[s] = r;
}

bool Is_Good(ll u){
    ll x = Get(0,1,1,n,in[u],out[u]),y = Get(1,1,1,n,in[u],out[u]);
    if (x < in[u]||y > out[u]) return 1;
    return 0;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define task "tst"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        //freopen(task".out", "w", stdout);
    }
    cin>>n>>k;
    for (ll i = 1;i < n;i++){
        cin>>x>>y; edge[i] = {x,y};
        g[x].push_back(y); g[y].push_back(x);
    }
    for (ll i = 1;i <= n;i++) cin>>grp[i],c[grp[i]].push_back(i);
    if (n == 2) return cout<<k - 1,0;
    DFS(1,0); memset(lab,-1,sizeof(lab));
    for (ll i = 1;i <= k;i++){
        ll mn = inf,mx = 0;
        for (auto j : c[i]) mn = min(mn,in[j]),mx = max(mx,out[j]);
        for (auto j : c[i]) upd(0,1,1,n,in[j],mn),upd(1,1,1,n,in[j],mx);
    }
    for (ll i = 2;i <= n;i++){
        if (Is_Good(i)) Union(i,par[i]);
        //cout<<i<<" "<<Is_Good(i)<<"\n";
    }
    for (ll i = 1;i < n;i++){
        ll x = edge[i].fs,y = edge[i].sc;
        if (Find(x) != Find(y)) cnt[Find(x)]++,cnt[Find(y)]++;
    }
    ll leaf = 0;
    for (ll i = 1;i <= n;i++) leaf += (cnt[i] == 1);
    cout<<(leaf + 1)/2;
}

Compilation message

mergers.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization "O2"
      | 
mergers.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
mergers.cpp: In function 'int main()':
mergers.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 25804 KB Output is correct
2 Correct 15 ms 25796 KB Output is correct
3 Correct 19 ms 25804 KB Output is correct
4 Correct 18 ms 25732 KB Output is correct
5 Correct 16 ms 25816 KB Output is correct
6 Correct 17 ms 25808 KB Output is correct
7 Correct 16 ms 25724 KB Output is correct
8 Correct 15 ms 25744 KB Output is correct
9 Correct 16 ms 25700 KB Output is correct
10 Correct 16 ms 25720 KB Output is correct
11 Correct 15 ms 25756 KB Output is correct
12 Correct 17 ms 25804 KB Output is correct
13 Correct 15 ms 25720 KB Output is correct
14 Correct 16 ms 25804 KB Output is correct
15 Correct 15 ms 25804 KB Output is correct
16 Correct 15 ms 25728 KB Output is correct
17 Correct 15 ms 25804 KB Output is correct
18 Correct 19 ms 25816 KB Output is correct
19 Correct 15 ms 25776 KB Output is correct
20 Correct 16 ms 25668 KB Output is correct
21 Correct 17 ms 25732 KB Output is correct
22 Correct 17 ms 25804 KB Output is correct
23 Correct 17 ms 25716 KB Output is correct
24 Correct 15 ms 25788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 25804 KB Output is correct
2 Correct 15 ms 25796 KB Output is correct
3 Correct 19 ms 25804 KB Output is correct
4 Correct 18 ms 25732 KB Output is correct
5 Correct 16 ms 25816 KB Output is correct
6 Correct 17 ms 25808 KB Output is correct
7 Correct 16 ms 25724 KB Output is correct
8 Correct 15 ms 25744 KB Output is correct
9 Correct 16 ms 25700 KB Output is correct
10 Correct 16 ms 25720 KB Output is correct
11 Correct 15 ms 25756 KB Output is correct
12 Correct 17 ms 25804 KB Output is correct
13 Correct 15 ms 25720 KB Output is correct
14 Correct 16 ms 25804 KB Output is correct
15 Correct 15 ms 25804 KB Output is correct
16 Correct 15 ms 25728 KB Output is correct
17 Correct 15 ms 25804 KB Output is correct
18 Correct 19 ms 25816 KB Output is correct
19 Correct 15 ms 25776 KB Output is correct
20 Correct 16 ms 25668 KB Output is correct
21 Correct 17 ms 25732 KB Output is correct
22 Correct 17 ms 25804 KB Output is correct
23 Correct 17 ms 25716 KB Output is correct
24 Correct 15 ms 25788 KB Output is correct
25 Correct 15 ms 25752 KB Output is correct
26 Correct 19 ms 26164 KB Output is correct
27 Correct 19 ms 26124 KB Output is correct
28 Correct 19 ms 26188 KB Output is correct
29 Correct 20 ms 26080 KB Output is correct
30 Correct 19 ms 26060 KB Output is correct
31 Correct 17 ms 25748 KB Output is correct
32 Correct 22 ms 26328 KB Output is correct
33 Correct 17 ms 25772 KB Output is correct
34 Correct 19 ms 26100 KB Output is correct
35 Correct 19 ms 26188 KB Output is correct
36 Correct 21 ms 26084 KB Output is correct
37 Correct 19 ms 26084 KB Output is correct
38 Correct 15 ms 25720 KB Output is correct
39 Correct 20 ms 26144 KB Output is correct
40 Correct 21 ms 26080 KB Output is correct
41 Correct 20 ms 25984 KB Output is correct
42 Correct 18 ms 26112 KB Output is correct
43 Correct 21 ms 26316 KB Output is correct
44 Correct 15 ms 25740 KB Output is correct
45 Correct 20 ms 26116 KB Output is correct
46 Correct 19 ms 26116 KB Output is correct
47 Correct 16 ms 25716 KB Output is correct
48 Correct 19 ms 26016 KB Output is correct
49 Correct 23 ms 26112 KB Output is correct
50 Correct 20 ms 26224 KB Output is correct
51 Correct 19 ms 26120 KB Output is correct
52 Correct 18 ms 26124 KB Output is correct
53 Correct 18 ms 26116 KB Output is correct
54 Correct 18 ms 26060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 25804 KB Output is correct
2 Correct 15 ms 25796 KB Output is correct
3 Correct 19 ms 25804 KB Output is correct
4 Correct 18 ms 25732 KB Output is correct
5 Correct 16 ms 25816 KB Output is correct
6 Correct 17 ms 25808 KB Output is correct
7 Correct 16 ms 25724 KB Output is correct
8 Correct 15 ms 25744 KB Output is correct
9 Correct 16 ms 25700 KB Output is correct
10 Correct 16 ms 25720 KB Output is correct
11 Correct 15 ms 25756 KB Output is correct
12 Correct 17 ms 25804 KB Output is correct
13 Correct 15 ms 25720 KB Output is correct
14 Correct 16 ms 25804 KB Output is correct
15 Correct 15 ms 25804 KB Output is correct
16 Correct 15 ms 25728 KB Output is correct
17 Correct 15 ms 25804 KB Output is correct
18 Correct 19 ms 25816 KB Output is correct
19 Correct 15 ms 25776 KB Output is correct
20 Correct 16 ms 25668 KB Output is correct
21 Correct 17 ms 25732 KB Output is correct
22 Correct 17 ms 25804 KB Output is correct
23 Correct 17 ms 25716 KB Output is correct
24 Correct 15 ms 25788 KB Output is correct
25 Correct 17 ms 25804 KB Output is correct
26 Correct 191 ms 35748 KB Output is correct
27 Correct 216 ms 35396 KB Output is correct
28 Correct 19 ms 26104 KB Output is correct
29 Correct 15 ms 25748 KB Output is correct
30 Correct 17 ms 25868 KB Output is correct
31 Correct 203 ms 35476 KB Output is correct
32 Correct 19 ms 26028 KB Output is correct
33 Correct 212 ms 42572 KB Output is correct
34 Correct 191 ms 35444 KB Output is correct
35 Correct 21 ms 25980 KB Output is correct
36 Correct 279 ms 36224 KB Output is correct
37 Correct 19 ms 26000 KB Output is correct
38 Correct 18 ms 26036 KB Output is correct
39 Correct 167 ms 35588 KB Output is correct
40 Correct 20 ms 26284 KB Output is correct
41 Correct 222 ms 35336 KB Output is correct
42 Correct 203 ms 37700 KB Output is correct
43 Correct 15 ms 25720 KB Output is correct
44 Correct 207 ms 42688 KB Output is correct
45 Correct 225 ms 39108 KB Output is correct
46 Correct 19 ms 26124 KB Output is correct
47 Correct 22 ms 26104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 171 ms 35644 KB Output is correct
2 Correct 199 ms 38892 KB Output is correct
3 Correct 19 ms 26060 KB Output is correct
4 Correct 19 ms 26084 KB Output is correct
5 Correct 15 ms 25764 KB Output is correct
6 Correct 16 ms 25808 KB Output is correct
7 Correct 19 ms 26060 KB Output is correct
8 Correct 225 ms 37112 KB Output is correct
9 Correct 18 ms 26060 KB Output is correct
10 Correct 197 ms 35876 KB Output is correct
11 Correct 17 ms 25716 KB Output is correct
12 Correct 203 ms 35848 KB Output is correct
13 Correct 220 ms 37060 KB Output is correct
14 Correct 239 ms 38672 KB Output is correct
15 Correct 176 ms 35580 KB Output is correct
16 Correct 19 ms 26060 KB Output is correct
17 Correct 16 ms 25744 KB Output is correct
18 Correct 198 ms 38668 KB Output is correct
19 Correct 270 ms 42596 KB Output is correct
20 Correct 22 ms 26188 KB Output is correct
21 Correct 16 ms 25720 KB Output is correct
22 Correct 203 ms 37308 KB Output is correct
23 Correct 23 ms 26060 KB Output is correct
24 Correct 214 ms 36288 KB Output is correct
25 Correct 247 ms 41256 KB Output is correct
26 Correct 20 ms 26188 KB Output is correct
27 Correct 19 ms 26228 KB Output is correct
28 Correct 19 ms 26120 KB Output is correct
29 Correct 19 ms 26080 KB Output is correct
30 Correct 19 ms 26136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 25804 KB Output is correct
2 Correct 15 ms 25796 KB Output is correct
3 Correct 19 ms 25804 KB Output is correct
4 Correct 18 ms 25732 KB Output is correct
5 Correct 16 ms 25816 KB Output is correct
6 Correct 17 ms 25808 KB Output is correct
7 Correct 16 ms 25724 KB Output is correct
8 Correct 15 ms 25744 KB Output is correct
9 Correct 16 ms 25700 KB Output is correct
10 Correct 16 ms 25720 KB Output is correct
11 Correct 15 ms 25756 KB Output is correct
12 Correct 17 ms 25804 KB Output is correct
13 Correct 15 ms 25720 KB Output is correct
14 Correct 16 ms 25804 KB Output is correct
15 Correct 15 ms 25804 KB Output is correct
16 Correct 15 ms 25728 KB Output is correct
17 Correct 15 ms 25804 KB Output is correct
18 Correct 19 ms 25816 KB Output is correct
19 Correct 15 ms 25776 KB Output is correct
20 Correct 16 ms 25668 KB Output is correct
21 Correct 17 ms 25732 KB Output is correct
22 Correct 17 ms 25804 KB Output is correct
23 Correct 17 ms 25716 KB Output is correct
24 Correct 15 ms 25788 KB Output is correct
25 Correct 15 ms 25752 KB Output is correct
26 Correct 19 ms 26164 KB Output is correct
27 Correct 19 ms 26124 KB Output is correct
28 Correct 19 ms 26188 KB Output is correct
29 Correct 20 ms 26080 KB Output is correct
30 Correct 19 ms 26060 KB Output is correct
31 Correct 17 ms 25748 KB Output is correct
32 Correct 22 ms 26328 KB Output is correct
33 Correct 17 ms 25772 KB Output is correct
34 Correct 19 ms 26100 KB Output is correct
35 Correct 19 ms 26188 KB Output is correct
36 Correct 21 ms 26084 KB Output is correct
37 Correct 19 ms 26084 KB Output is correct
38 Correct 15 ms 25720 KB Output is correct
39 Correct 20 ms 26144 KB Output is correct
40 Correct 21 ms 26080 KB Output is correct
41 Correct 20 ms 25984 KB Output is correct
42 Correct 18 ms 26112 KB Output is correct
43 Correct 21 ms 26316 KB Output is correct
44 Correct 15 ms 25740 KB Output is correct
45 Correct 20 ms 26116 KB Output is correct
46 Correct 19 ms 26116 KB Output is correct
47 Correct 16 ms 25716 KB Output is correct
48 Correct 19 ms 26016 KB Output is correct
49 Correct 23 ms 26112 KB Output is correct
50 Correct 20 ms 26224 KB Output is correct
51 Correct 19 ms 26120 KB Output is correct
52 Correct 18 ms 26124 KB Output is correct
53 Correct 18 ms 26116 KB Output is correct
54 Correct 18 ms 26060 KB Output is correct
55 Correct 17 ms 25804 KB Output is correct
56 Correct 191 ms 35748 KB Output is correct
57 Correct 216 ms 35396 KB Output is correct
58 Correct 19 ms 26104 KB Output is correct
59 Correct 15 ms 25748 KB Output is correct
60 Correct 17 ms 25868 KB Output is correct
61 Correct 203 ms 35476 KB Output is correct
62 Correct 19 ms 26028 KB Output is correct
63 Correct 212 ms 42572 KB Output is correct
64 Correct 191 ms 35444 KB Output is correct
65 Correct 21 ms 25980 KB Output is correct
66 Correct 279 ms 36224 KB Output is correct
67 Correct 19 ms 26000 KB Output is correct
68 Correct 18 ms 26036 KB Output is correct
69 Correct 167 ms 35588 KB Output is correct
70 Correct 20 ms 26284 KB Output is correct
71 Correct 222 ms 35336 KB Output is correct
72 Correct 203 ms 37700 KB Output is correct
73 Correct 15 ms 25720 KB Output is correct
74 Correct 207 ms 42688 KB Output is correct
75 Correct 225 ms 39108 KB Output is correct
76 Correct 19 ms 26124 KB Output is correct
77 Correct 22 ms 26104 KB Output is correct
78 Correct 171 ms 35644 KB Output is correct
79 Correct 199 ms 38892 KB Output is correct
80 Correct 19 ms 26060 KB Output is correct
81 Correct 19 ms 26084 KB Output is correct
82 Correct 15 ms 25764 KB Output is correct
83 Correct 16 ms 25808 KB Output is correct
84 Correct 19 ms 26060 KB Output is correct
85 Correct 225 ms 37112 KB Output is correct
86 Correct 18 ms 26060 KB Output is correct
87 Correct 197 ms 35876 KB Output is correct
88 Correct 17 ms 25716 KB Output is correct
89 Correct 203 ms 35848 KB Output is correct
90 Correct 220 ms 37060 KB Output is correct
91 Correct 239 ms 38672 KB Output is correct
92 Correct 176 ms 35580 KB Output is correct
93 Correct 19 ms 26060 KB Output is correct
94 Correct 16 ms 25744 KB Output is correct
95 Correct 198 ms 38668 KB Output is correct
96 Correct 270 ms 42596 KB Output is correct
97 Correct 22 ms 26188 KB Output is correct
98 Correct 16 ms 25720 KB Output is correct
99 Correct 203 ms 37308 KB Output is correct
100 Correct 23 ms 26060 KB Output is correct
101 Correct 214 ms 36288 KB Output is correct
102 Correct 247 ms 41256 KB Output is correct
103 Correct 20 ms 26188 KB Output is correct
104 Correct 19 ms 26228 KB Output is correct
105 Correct 19 ms 26120 KB Output is correct
106 Correct 19 ms 26080 KB Output is correct
107 Correct 19 ms 26136 KB Output is correct
108 Correct 1346 ms 75944 KB Output is correct
109 Correct 1581 ms 86168 KB Output is correct
110 Correct 1593 ms 97328 KB Output is correct
111 Correct 1834 ms 106832 KB Output is correct
112 Correct 1912 ms 96980 KB Output is correct
113 Correct 1382 ms 83572 KB Output is correct
114 Correct 1397 ms 68244 KB Output is correct
115 Correct 1447 ms 68124 KB Output is correct
116 Correct 1563 ms 72608 KB Output is correct
117 Correct 1620 ms 83608 KB Output is correct
118 Correct 1270 ms 69940 KB Output is correct
119 Correct 1561 ms 83584 KB Output is correct
120 Correct 1785 ms 97544 KB Output is correct
121 Correct 1579 ms 83628 KB Output is correct
122 Correct 1548 ms 77248 KB Output is correct
123 Correct 1352 ms 84952 KB Output is correct
124 Correct 1768 ms 82528 KB Output is correct