답안 #124308

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124308 2019-07-03T01:26:08 Z duality Unique Cities (JOI19_ho_t5) C++11
100 / 100
1924 ms 110444 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

int N,logn;
vi adjList[200000];
int C[200000];
int parent[200000][18],height[200000],node[200000];
int depth[200000],disc[200000],fin[200000],inv[200000],num = 0;
int doDFS(int u,int p,int d) {
    int i;
    parent[u][0] = p,height[u] = 0,node[u] = u;
    depth[u] = d,disc[u] = num++,inv[num-1] = u;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) {
            doDFS(v,u,d+1);
            if (height[v]+1 > height[u]) {
                height[u] = height[v]+1,node[u] = node[v];
                if (!(height[u] & 1)) node[u] = parent[node[u]][0];
            }
        }
    }
    fin[u] = num;
    return 0;
}
int moveToward(int u,int v) {
    if (u == -1) return 0;
    if ((disc[v] >= disc[u]) && (disc[v] < fin[u])) {
        int i;
        for (i = logn-1; i >= 0; i--) {
            if ((parent[v][i] != -1) && (depth[parent[v][i]] > depth[u])) v = parent[v][i];
        }
        return v;
    }
    else return parent[u][0];
}
int height2[200000],node2[200000];
int doDFS2(int u,int p) {
    int i;
    int m1 = height2[u],m2 = height2[u],p1 = u,p2 = u;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) {
            if (height[v]+1 >= m1) m2 = m1,p2 = p1,m1 = height[v]+1,p1 = v;
            else if (height[v]+1 >= m2) m2 = height[v]+1,p2 = v;
        }
    }
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) {
            if (v == p1) height2[v] = m2+1,node2[v] = (p2 == u) ? node2[p2]:node[p2];
            else height2[v] = m1+1,node2[v] = (p1 == u) ? node2[p1]:node[p1];
            if (height2[v] & 1) node2[v] = moveToward(node2[v],v);
            doDFS2(v,u);
        }
    }
    return 0;
}
vpii cand,queries[200000];
int mm[400000];
int tree[400000],lazy[200000];
int apply(int p,int num) {
    tree[p] += num;
    if (p < N) lazy[p] += num;
    return 0;
}
int build(int p) {
    while (p > 1) p >>= 1,tree[p] = max(tree[p << 1],tree[(p << 1) | 1])+lazy[p];
    return 0;
}
int push(int p) {
    int i;
    for (i = logn; i > 0; i--) {
        int j = p >> i;
        if (lazy[j] != 0) {
            apply(j << 1,lazy[j]);
            apply((j << 1) | 1,lazy[j]);
            lazy[j] = 0;
        }
    }
    return 0;
}
int update(int as,int ae,int num) {
    as += N,ae += N+1;
    int oas = as,oae = ae;
    while (as < ae) {
        if (as & 1) apply(as++,num);
        if (ae & 1) apply(--ae,num);
        as >>= 1,ae >>= 1;
    }
    build(oas),build(oae-1);
    return 0;
}
int query(int qs,int qe) {
    qs += N,qe += N+1;
    push(qs),push(qe-1);
    int ans = 0;
    while (qs < qe) {
        if (qs & 1) ans = max(ans,tree[qs++]);
        if (qe & 1) ans = max(ans,tree[--qe]);
        qs >>= 1,qe >>= 1;
    }
    return ans;
}
int doDFS3(int u,int p) {
    int i;
    for (i = 0; i < queries[u].size(); i++) {
        int v = queries[u][i].first;
        mm[queries[u][i].second] -= query(disc[v],disc[v]);
        if ((disc[u] >= disc[v]) && (disc[u] < fin[v])) {
            v = moveToward(v,u);
            mm[queries[u][i].second] += query(disc[v],fin[v]-1);
        }
        else {
            int m = query(fin[v],N-1);
            if (m >= -mm[queries[u][i].second]) mm[queries[u][i].second] = 0;
            else mm[queries[u][i].second] += max(m,query(0,disc[v]-1));
        }
    }
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) {
            update(0,N-1,1),update(disc[v],fin[v]-1,-2);
            doDFS3(v,u);
            update(0,N-1,-1),update(disc[v],fin[v]-1,2);
        }
    }
    return 0;
}
int c[200000],n = 0;
vi tree2[1 << 19];
int ans[200000];
int add(int s,int e,int as,int ae,int i,int num) {
    if ((s > ae) || (e < as)) return 0;
    else if ((s >= as) && (e <= ae)) {
        tree2[i].pb(num);
        return 0;
    }

    int mid = (s+e) / 2;
    add(s,mid,as,ae,2*i+1,num),add(mid+1,e,as,ae,2*i+2,num);
    return 0;
}
int process(int s,int e,int i) {
    if (s == e) {
        int j;
        for (j = 0; j < tree2[i].size(); j++) {
            if (c[tree2[i][j]] == 0) n++;
            c[tree2[i][j]]++;
        }
        ans[inv[s]] = n;
        for (j = 0; j < tree2[i].size(); j++) {
            if (c[tree2[i][j]] == 1) n--;
            c[tree2[i][j]]--;
        }
        return 0;
    }

    int j,mid = (s+e) / 2;
    for (j = 0; j < tree2[i].size(); j++) {
        if (c[tree2[i][j]] == 0) n++;
        c[tree2[i][j]]++;
    }
    process(s,mid,2*i+1),process(mid+1,e,2*i+2);
    for (j = 0; j < tree2[i].size(); j++) {
        if (c[tree2[i][j]] == 1) n--;
        c[tree2[i][j]]--;
    }
    return 0;
}
int main() {
    int i;
    int M,A,B;
    scanf("%d %d",&N,&M);
    for (i = 0; i < N-1; i++) {
        scanf("%d %d",&A,&B);
        A--,B--;
        adjList[A].pb(B);
        adjList[B].pb(A);
    }
    for (i = 0; i < N; i++) scanf("%d",&C[i]),C[i]--;

    int j;
    doDFS(0,-1,0);
    for (i = 1; (1 << i) < N; i++) {
        for (j = 0; j < N; j++) {
            if (parent[j][i-1] != -1) parent[j][i] = parent[parent[j][i-1]][i-1];
            else parent[j][i] = -1;
        }
    }
    logn = i;
    height2[0] = 0,node2[0] = -1;
    doDFS2(0,-1);
    for (i = 1; i < N; i++) {
        cand.pb(mp(parent[i][0],node[i]));
        cand.pb(mp(i,node2[i]));
    }
    for (i = 0; i < cand.size(); i++) queries[cand[i].second].pb(mp(cand[i].first,i));
    for (i = 0; i < N; i++) tree[i+N] = depth[inv[i]];
    for (i = N-1; i > 0; i--) tree[i] = max(tree[i << 1],tree[(i << 1) | 1]);
    doDFS3(0,-1);
    for (i = 0; i < cand.size(); i++) {
        if (mm[i] < 0) {
            int u = cand[i].first,v = cand[i].second;
            if ((disc[u] >= disc[v]) && (disc[u] < fin[v])) {
                v = moveToward(v,u);
                add(0,N-1,fin[v],N-1,0,C[u]),add(0,N-1,0,disc[v]-1,0,C[u]);
            }
            else add(0,N-1,disc[v],fin[v]-1,0,C[u]);
        }
    }
    process(0,N-1,0);
    for (i = 0; i < N; i++) printf("%d\n",ans[i]);

    return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'int doDFS(int, int, int)':
joi2019_ho_t5.cpp:67:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int doDFS2(int, int)':
joi2019_ho_t5.cpp:95:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:102:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int doDFS3(int, int)':
joi2019_ho_t5.cpp:161:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < queries[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:174:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int process(int, int, int)':
joi2019_ho_t5.cpp:201:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j < tree2[i].size(); j++) {
                     ~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:206:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j < tree2[i].size(); j++) {
                     ~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:214:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (j = 0; j < tree2[i].size(); j++) {
                 ~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:219:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (j = 0; j < tree2[i].size(); j++) {
                 ~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:252:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < cand.size(); i++) queries[cand[i].second].pb(mp(cand[i].first,i));
                 ~~^~~~~~~~~~~~~
joi2019_ho_t5.cpp:256:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < cand.size(); i++) {
                 ~~^~~~~~~~~~~~~
joi2019_ho_t5.cpp:228:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&N,&M);
     ~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:230:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&A,&B);
         ~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:235:46: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&C[i]),C[i]--;
                             ~~~~~~~~~~~~~~~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 22136 KB Output is correct
2 Correct 25 ms 22540 KB Output is correct
3 Correct 24 ms 22364 KB Output is correct
4 Correct 26 ms 22520 KB Output is correct
5 Correct 27 ms 22520 KB Output is correct
6 Correct 28 ms 22748 KB Output is correct
7 Correct 26 ms 22648 KB Output is correct
8 Correct 25 ms 22620 KB Output is correct
9 Correct 26 ms 22620 KB Output is correct
10 Correct 25 ms 22548 KB Output is correct
11 Correct 26 ms 22520 KB Output is correct
12 Correct 26 ms 22648 KB Output is correct
13 Correct 28 ms 22800 KB Output is correct
14 Correct 27 ms 22648 KB Output is correct
15 Correct 26 ms 22648 KB Output is correct
16 Correct 25 ms 22648 KB Output is correct
17 Correct 27 ms 22844 KB Output is correct
18 Correct 25 ms 22648 KB Output is correct
19 Correct 26 ms 22620 KB Output is correct
20 Correct 27 ms 22776 KB Output is correct
21 Correct 25 ms 22648 KB Output is correct
22 Correct 25 ms 22620 KB Output is correct
23 Correct 25 ms 22648 KB Output is correct
24 Correct 25 ms 22520 KB Output is correct
25 Correct 25 ms 22520 KB Output is correct
26 Correct 25 ms 22648 KB Output is correct
27 Correct 33 ms 22776 KB Output is correct
28 Correct 32 ms 22748 KB Output is correct
29 Correct 30 ms 22588 KB Output is correct
30 Correct 29 ms 22608 KB Output is correct
31 Correct 30 ms 22776 KB Output is correct
32 Correct 33 ms 22620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 453 ms 48700 KB Output is correct
2 Correct 954 ms 69312 KB Output is correct
3 Correct 124 ms 30420 KB Output is correct
4 Correct 848 ms 67808 KB Output is correct
5 Correct 1694 ms 98396 KB Output is correct
6 Correct 1265 ms 81824 KB Output is correct
7 Correct 821 ms 67900 KB Output is correct
8 Correct 905 ms 69080 KB Output is correct
9 Correct 948 ms 68472 KB Output is correct
10 Correct 926 ms 68608 KB Output is correct
11 Correct 731 ms 71768 KB Output is correct
12 Correct 1528 ms 89664 KB Output is correct
13 Correct 1258 ms 83064 KB Output is correct
14 Correct 1202 ms 81508 KB Output is correct
15 Correct 698 ms 71840 KB Output is correct
16 Correct 1343 ms 94896 KB Output is correct
17 Correct 1135 ms 81304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 629 ms 58344 KB Output is correct
2 Correct 1592 ms 99060 KB Output is correct
3 Correct 141 ms 31328 KB Output is correct
4 Correct 845 ms 67996 KB Output is correct
5 Correct 1662 ms 98864 KB Output is correct
6 Correct 1166 ms 79544 KB Output is correct
7 Correct 830 ms 68008 KB Output is correct
8 Correct 954 ms 71360 KB Output is correct
9 Correct 948 ms 70364 KB Output is correct
10 Correct 892 ms 69464 KB Output is correct
11 Correct 795 ms 69392 KB Output is correct
12 Correct 1738 ms 98200 KB Output is correct
13 Correct 1164 ms 80912 KB Output is correct
14 Correct 1223 ms 81940 KB Output is correct
15 Correct 722 ms 71960 KB Output is correct
16 Correct 1355 ms 97800 KB Output is correct
17 Correct 1167 ms 79840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 22136 KB Output is correct
2 Correct 25 ms 22540 KB Output is correct
3 Correct 24 ms 22364 KB Output is correct
4 Correct 26 ms 22520 KB Output is correct
5 Correct 27 ms 22520 KB Output is correct
6 Correct 28 ms 22748 KB Output is correct
7 Correct 26 ms 22648 KB Output is correct
8 Correct 25 ms 22620 KB Output is correct
9 Correct 26 ms 22620 KB Output is correct
10 Correct 25 ms 22548 KB Output is correct
11 Correct 26 ms 22520 KB Output is correct
12 Correct 26 ms 22648 KB Output is correct
13 Correct 28 ms 22800 KB Output is correct
14 Correct 27 ms 22648 KB Output is correct
15 Correct 26 ms 22648 KB Output is correct
16 Correct 25 ms 22648 KB Output is correct
17 Correct 27 ms 22844 KB Output is correct
18 Correct 25 ms 22648 KB Output is correct
19 Correct 26 ms 22620 KB Output is correct
20 Correct 27 ms 22776 KB Output is correct
21 Correct 25 ms 22648 KB Output is correct
22 Correct 25 ms 22620 KB Output is correct
23 Correct 25 ms 22648 KB Output is correct
24 Correct 25 ms 22520 KB Output is correct
25 Correct 25 ms 22520 KB Output is correct
26 Correct 25 ms 22648 KB Output is correct
27 Correct 33 ms 22776 KB Output is correct
28 Correct 32 ms 22748 KB Output is correct
29 Correct 30 ms 22588 KB Output is correct
30 Correct 29 ms 22608 KB Output is correct
31 Correct 30 ms 22776 KB Output is correct
32 Correct 33 ms 22620 KB Output is correct
33 Correct 453 ms 48700 KB Output is correct
34 Correct 954 ms 69312 KB Output is correct
35 Correct 124 ms 30420 KB Output is correct
36 Correct 848 ms 67808 KB Output is correct
37 Correct 1694 ms 98396 KB Output is correct
38 Correct 1265 ms 81824 KB Output is correct
39 Correct 821 ms 67900 KB Output is correct
40 Correct 905 ms 69080 KB Output is correct
41 Correct 948 ms 68472 KB Output is correct
42 Correct 926 ms 68608 KB Output is correct
43 Correct 731 ms 71768 KB Output is correct
44 Correct 1528 ms 89664 KB Output is correct
45 Correct 1258 ms 83064 KB Output is correct
46 Correct 1202 ms 81508 KB Output is correct
47 Correct 698 ms 71840 KB Output is correct
48 Correct 1343 ms 94896 KB Output is correct
49 Correct 1135 ms 81304 KB Output is correct
50 Correct 629 ms 58344 KB Output is correct
51 Correct 1592 ms 99060 KB Output is correct
52 Correct 141 ms 31328 KB Output is correct
53 Correct 845 ms 67996 KB Output is correct
54 Correct 1662 ms 98864 KB Output is correct
55 Correct 1166 ms 79544 KB Output is correct
56 Correct 830 ms 68008 KB Output is correct
57 Correct 954 ms 71360 KB Output is correct
58 Correct 948 ms 70364 KB Output is correct
59 Correct 892 ms 69464 KB Output is correct
60 Correct 795 ms 69392 KB Output is correct
61 Correct 1738 ms 98200 KB Output is correct
62 Correct 1164 ms 80912 KB Output is correct
63 Correct 1223 ms 81940 KB Output is correct
64 Correct 722 ms 71960 KB Output is correct
65 Correct 1355 ms 97800 KB Output is correct
66 Correct 1167 ms 79840 KB Output is correct
67 Correct 99 ms 28300 KB Output is correct
68 Correct 775 ms 62060 KB Output is correct
69 Correct 661 ms 57380 KB Output is correct
70 Correct 837 ms 67276 KB Output is correct
71 Correct 1764 ms 101664 KB Output is correct
72 Correct 1175 ms 80592 KB Output is correct
73 Correct 839 ms 67220 KB Output is correct
74 Correct 1012 ms 71284 KB Output is correct
75 Correct 937 ms 68640 KB Output is correct
76 Correct 902 ms 68308 KB Output is correct
77 Correct 792 ms 69064 KB Output is correct
78 Correct 1537 ms 90460 KB Output is correct
79 Correct 1554 ms 92908 KB Output is correct
80 Correct 1196 ms 80520 KB Output is correct
81 Correct 686 ms 71828 KB Output is correct
82 Correct 1686 ms 99776 KB Output is correct
83 Correct 1145 ms 80380 KB Output is correct
84 Correct 878 ms 67244 KB Output is correct
85 Correct 1924 ms 99108 KB Output is correct
86 Correct 1256 ms 77956 KB Output is correct
87 Correct 819 ms 67172 KB Output is correct
88 Correct 980 ms 71720 KB Output is correct
89 Correct 892 ms 69124 KB Output is correct
90 Correct 877 ms 68756 KB Output is correct
91 Correct 778 ms 68928 KB Output is correct
92 Correct 1874 ms 107560 KB Output is correct
93 Correct 1214 ms 77484 KB Output is correct
94 Correct 970 ms 72420 KB Output is correct
95 Correct 688 ms 71748 KB Output is correct
96 Correct 1276 ms 96092 KB Output is correct
97 Correct 1097 ms 81124 KB Output is correct
98 Correct 856 ms 68112 KB Output is correct
99 Correct 1913 ms 110444 KB Output is correct
100 Correct 1163 ms 82188 KB Output is correct
101 Correct 816 ms 67688 KB Output is correct
102 Correct 945 ms 70548 KB Output is correct
103 Correct 894 ms 69596 KB Output is correct
104 Correct 808 ms 68700 KB Output is correct
105 Correct 741 ms 69472 KB Output is correct
106 Correct 1331 ms 85984 KB Output is correct
107 Correct 1316 ms 90968 KB Output is correct
108 Correct 998 ms 75488 KB Output is correct
109 Correct 686 ms 71896 KB Output is correct
110 Correct 1284 ms 97056 KB Output is correct
111 Correct 1091 ms 81148 KB Output is correct