Submission #124282

# Submission time Handle Problem Language Result Execution time Memory
124282 2019-07-02T23:51:27 Z duality Unique Cities (JOI19_ho_t5) C++11
4 / 100
2000 ms 79096 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 ----------

vi adjList[200000];
int C[200000];
int parent[200000][18],height[200000],node[200000],half[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] = half[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],half[u] = half[v];
                if (!(height[u] & 1)) half[u] = parent[half[u]][0];
            }
        }
    }
    fin[u] = num;
    return 0;
}
int logn;
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],half2[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;
                if (p2 == u) {
                    node2[v] = node2[p2],half2[v] = half2[p2];
                    if (height2[v] & 1) half2[v] = moveToward(half2[v],v);
                }
                else {
                    node2[v] = node[p2],half2[v] = half[p2];
                    if (height2[v] & 1) half2[v] = moveToward(half2[v],v);
                }
            }
            else {
                height2[v] = m1+1;
                if (p1 == u) {
                    node2[v] = node2[p1],half2[v] = half2[p1];
                    if (height2[v] & 1) half2[v] = moveToward(half2[v],v);
                }
                else {
                    node2[v] = node[p1],half2[v] = half[p1];
                    if (height2[v] & 1) half2[v] = moveToward(half2[v],v);
                }
            }
            doDFS2(v,u);
        }
    }
    return 0;
}
vpii cand;
int dd = 0,mm = 0;
int doDFS3(int u,int p,int d,int x) {
    int i;
    if (u != x) mm = max(mm,d);
    else {
        dd = d;
        return 0;
    }
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) doDFS3(v,u,d+1,x);
    }
    return 0;
}
set<int> S[200000];
int main() {
    int i;
    int N,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]);

    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] = node2[0] = 0,half2[0] = -1;
    doDFS2(0,-1);
    for (i = 1; i < N; i++) {
        cand.pb(mp(parent[i][0],half[i]));
        cand.pb(mp(i,half2[i]));
    }
    for (i = 0; i < cand.size(); i++) {
        dd = mm = 0;
        doDFS3(cand[i].second,-1,0,cand[i].first);
        if (mm < dd) {
            int u = cand[i].first,v = cand[i].second;
            //cout<<u<<","<<v<<endl;
            if ((disc[u] >= disc[v]) && (disc[u] < fin[v])) {
                v = moveToward(v,u);
                for (j = fin[v]; j < N; j++) S[j].insert(C[u]);
                for (j = 0; j < disc[v]; j++) S[j].insert(C[u]);
            }
            else {
                for (j = disc[v]; j < fin[v]; j++) S[j].insert(C[u]);
            }
        }
    }
    for (i = 0; i < N; i++) printf("%d\n",S[disc[i]].size());

    printArr(half,N);
    printArr(half2,N);
    printArr(height,N);
    printArr(height2,N);

    return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'int doDFS(int, int, int)':
joi2019_ho_t5.cpp:66: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, int, int)':
joi2019_ho_t5.cpp:141: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 main()':
joi2019_ho_t5.cpp:175:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < cand.size(); i++) {
                 ~~^~~~~~~~~~~~~
joi2019_ho_t5.cpp:191:60: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::set<int>::size_type {aka long unsigned int}' [-Wformat=]
     for (i = 0; i < N; i++) printf("%d\n",S[disc[i]].size());
                                           ~~~~~~~~~~~~~~~~~^
joi2019_ho_t5.cpp:151: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:153: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:158:34: 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]);
                             ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 61 ms 14840 KB Output is correct
3 Correct 103 ms 30200 KB Output is correct
4 Correct 99 ms 19960 KB Output is correct
5 Correct 73 ms 14840 KB Output is correct
6 Correct 134 ms 15480 KB Output is correct
7 Correct 93 ms 14968 KB Output is correct
8 Correct 63 ms 14940 KB Output is correct
9 Correct 80 ms 15032 KB Output is correct
10 Correct 76 ms 15100 KB Output is correct
11 Correct 77 ms 15248 KB Output is correct
12 Correct 52 ms 14968 KB Output is correct
13 Correct 126 ms 15412 KB Output is correct
14 Correct 79 ms 14968 KB Output is correct
15 Correct 76 ms 15224 KB Output is correct
16 Correct 46 ms 14968 KB Output is correct
17 Correct 92 ms 15488 KB Output is correct
18 Correct 92 ms 14992 KB Output is correct
19 Correct 78 ms 14840 KB Output is correct
20 Correct 476 ms 79096 KB Output is correct
21 Correct 124 ms 22576 KB Output is correct
22 Correct 67 ms 15020 KB Output is correct
23 Correct 82 ms 15480 KB Output is correct
24 Correct 76 ms 15356 KB Output is correct
25 Correct 76 ms 15128 KB Output is correct
26 Correct 52 ms 14904 KB Output is correct
27 Correct 337 ms 56924 KB Output is correct
28 Correct 220 ms 40312 KB Output is correct
29 Correct 98 ms 19988 KB Output is correct
30 Correct 50 ms 14968 KB Output is correct
31 Correct 188 ms 28408 KB Output is correct
32 Correct 118 ms 22644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2070 ms 33496 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2061 ms 41700 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 61 ms 14840 KB Output is correct
3 Correct 103 ms 30200 KB Output is correct
4 Correct 99 ms 19960 KB Output is correct
5 Correct 73 ms 14840 KB Output is correct
6 Correct 134 ms 15480 KB Output is correct
7 Correct 93 ms 14968 KB Output is correct
8 Correct 63 ms 14940 KB Output is correct
9 Correct 80 ms 15032 KB Output is correct
10 Correct 76 ms 15100 KB Output is correct
11 Correct 77 ms 15248 KB Output is correct
12 Correct 52 ms 14968 KB Output is correct
13 Correct 126 ms 15412 KB Output is correct
14 Correct 79 ms 14968 KB Output is correct
15 Correct 76 ms 15224 KB Output is correct
16 Correct 46 ms 14968 KB Output is correct
17 Correct 92 ms 15488 KB Output is correct
18 Correct 92 ms 14992 KB Output is correct
19 Correct 78 ms 14840 KB Output is correct
20 Correct 476 ms 79096 KB Output is correct
21 Correct 124 ms 22576 KB Output is correct
22 Correct 67 ms 15020 KB Output is correct
23 Correct 82 ms 15480 KB Output is correct
24 Correct 76 ms 15356 KB Output is correct
25 Correct 76 ms 15128 KB Output is correct
26 Correct 52 ms 14904 KB Output is correct
27 Correct 337 ms 56924 KB Output is correct
28 Correct 220 ms 40312 KB Output is correct
29 Correct 98 ms 19988 KB Output is correct
30 Correct 50 ms 14968 KB Output is correct
31 Correct 188 ms 28408 KB Output is correct
32 Correct 118 ms 22644 KB Output is correct
33 Execution timed out 2070 ms 33496 KB Time limit exceeded
34 Halted 0 ms 0 KB -