Submission #128450

# Submission time Handle Problem Language Result Execution time Memory
128450 2019-07-10T23:41:26 Z tri Unique Cities (JOI19_ho_t5) C++14
100 / 100
1393 ms 49472 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace debug {
    const int DEBUG = true;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"); }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;


vector<vi> aL;
vi dist;

namespace treeDiameter {

    void calcDist(int cV, int pV) {
        for (int aV : aL[cV]) {
            if (aV != pV) {
                dist[aV] = dist[cV] + 1;
                calcDist(aV, cV);
            }
        }
    }

    pi fDiameter() {
        dist[0] = 0;
        calcDist(0, -1);
        int end1 = max_element(dist.begin(), dist.end()) - dist.begin();

        dist[end1] = 0;
        calcDist(end1, -1);
        int end2 = max_element(dist.begin(), dist.end()) - dist.begin();
        return {end1, end2};
    }
}
using namespace treeDiameter;

vi cl;

vi subDep;
vi ans;

vi cCnt;
int tCnt = 0;

stack<int> stk;

void addCl(int x) {
    if (++cCnt[x] == 1) {
        tCnt++;
    }
}

void remCl(int x) {
    if (--cCnt[x] == 0) {
        tCnt--;
    }
}

void calcMaxSubDep(int cV, int pV) {
    int cM = 0;
    for (int aV : aL[cV]) {
        if (aV != pV) {
            calcMaxSubDep(aV, cV);
            cM = max(cM, subDep[aV]);
        }
    }
    subDep[cV] = cM + 1;
}


void cntPath(int cV, int pV) {
//    ps("frame", cV);
    vector<pi> ch;
    for (int aV : aL[cV]) {
        if (aV != pV) {
            ch.pb({subDep[aV], aV});
        }
    }

    if (ch.size()) {
        swap(ch[0], *max_element(ch.begin(), ch.end()));
        int maxSibDep = ch.size() > 1 ? (*max_element(ch.begin() + 1, ch.end())).f : 0;
//        ps(maxSibDep);

        for (pi cCh: ch) {
            int aV = cCh.s;

            while (stk.size() && dist[stk.top()] >= dist[cV] - maxSibDep) {
//                ps("removed1", stk.top(), dist[stk.top()], dist[cV] - maxSibDep);
                remCl(cl[stk.top()]);
                stk.pop();
            }
            addCl(cl[cV]);
            stk.push(cV);
//            ps("added", cV);

            cntPath(aV, cV);

            if (stk.size() && stk.top() == cV) {
//                ps("removed", stk.top());
                remCl(cl[stk.top()]);
                stk.pop();
            }
            maxSibDep = ch[0].f;
        }
        while (stk.size() && dist[stk.top()] >= dist[cV] - maxSibDep) {
//            ps("removed", stk.top());
            remCl(cl[stk.top()]);
            stk.pop();
        }
    }
    ans[cV] = max(ans[cV], tCnt);
//    ps(cCnt);
}

int main() {
    int N, M;
    cin >> N >> M;
    aL.resize(N);
    cl.resize(N);
    cCnt.resize(M, 0);

    for (int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        aL[a].pb(b);
        aL[b].pb(a);
    }

    for (int i = 0; i < N; i++) {
        cin >> cl[i];
        cl[i]--;
    }

//    ps("input");

    dist.resize(N);
    pi diameter = fDiameter();

//    ps("diameter");

    subDep.resize(N);
    ans.resize(N);

//    ps(diameter);
//    ps(dist);

    calcMaxSubDep(diameter.f, -1);

    cntPath(diameter.f, -1);

//    ps("part1");

    dist[diameter.s] = 0;
    calcDist(diameter.s, -1);
    calcMaxSubDep(diameter.s, -1);
    cntPath(diameter.s, -1);

    for (int i = 0; i < N; i++) {
        cout << ans[i] << endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 10 ms 496 KB Output is correct
3 Correct 6 ms 504 KB Output is correct
4 Correct 10 ms 636 KB Output is correct
5 Correct 10 ms 504 KB Output is correct
6 Correct 11 ms 760 KB Output is correct
7 Correct 10 ms 632 KB Output is correct
8 Correct 10 ms 504 KB Output is correct
9 Correct 10 ms 504 KB Output is correct
10 Correct 10 ms 504 KB Output is correct
11 Correct 10 ms 504 KB Output is correct
12 Correct 10 ms 508 KB Output is correct
13 Correct 11 ms 760 KB Output is correct
14 Correct 10 ms 632 KB Output is correct
15 Correct 10 ms 632 KB Output is correct
16 Correct 10 ms 504 KB Output is correct
17 Correct 10 ms 632 KB Output is correct
18 Correct 10 ms 632 KB Output is correct
19 Correct 10 ms 504 KB Output is correct
20 Correct 12 ms 760 KB Output is correct
21 Correct 11 ms 632 KB Output is correct
22 Correct 11 ms 504 KB Output is correct
23 Correct 11 ms 504 KB Output is correct
24 Correct 11 ms 504 KB Output is correct
25 Correct 11 ms 504 KB Output is correct
26 Correct 10 ms 504 KB Output is correct
27 Correct 11 ms 632 KB Output is correct
28 Correct 11 ms 664 KB Output is correct
29 Correct 11 ms 632 KB Output is correct
30 Correct 10 ms 504 KB Output is correct
31 Correct 11 ms 760 KB Output is correct
32 Correct 11 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 666 ms 10440 KB Output is correct
2 Correct 749 ms 27000 KB Output is correct
3 Correct 150 ms 4856 KB Output is correct
4 Correct 1184 ms 18168 KB Output is correct
5 Correct 1299 ms 46784 KB Output is correct
6 Correct 1295 ms 32228 KB Output is correct
7 Correct 1100 ms 18328 KB Output is correct
8 Correct 1301 ms 21156 KB Output is correct
9 Correct 1168 ms 19936 KB Output is correct
10 Correct 1158 ms 19884 KB Output is correct
11 Correct 950 ms 21804 KB Output is correct
12 Correct 1300 ms 35960 KB Output is correct
13 Correct 1136 ms 32372 KB Output is correct
14 Correct 1201 ms 31624 KB Output is correct
15 Correct 930 ms 21756 KB Output is correct
16 Correct 1218 ms 38512 KB Output is correct
17 Correct 1230 ms 32804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 949 ms 15480 KB Output is correct
2 Correct 1358 ms 47224 KB Output is correct
3 Correct 182 ms 5752 KB Output is correct
4 Correct 1227 ms 19620 KB Output is correct
5 Correct 1386 ms 49472 KB Output is correct
6 Correct 1375 ms 34236 KB Output is correct
7 Correct 1153 ms 19776 KB Output is correct
8 Correct 1291 ms 25720 KB Output is correct
9 Correct 1228 ms 23928 KB Output is correct
10 Correct 1240 ms 21836 KB Output is correct
11 Correct 1107 ms 21336 KB Output is correct
12 Correct 1361 ms 43280 KB Output is correct
13 Correct 1332 ms 31936 KB Output is correct
14 Correct 1393 ms 32664 KB Output is correct
15 Correct 963 ms 23628 KB Output is correct
16 Correct 1245 ms 40412 KB Output is correct
17 Correct 1343 ms 34376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 10 ms 496 KB Output is correct
3 Correct 6 ms 504 KB Output is correct
4 Correct 10 ms 636 KB Output is correct
5 Correct 10 ms 504 KB Output is correct
6 Correct 11 ms 760 KB Output is correct
7 Correct 10 ms 632 KB Output is correct
8 Correct 10 ms 504 KB Output is correct
9 Correct 10 ms 504 KB Output is correct
10 Correct 10 ms 504 KB Output is correct
11 Correct 10 ms 504 KB Output is correct
12 Correct 10 ms 508 KB Output is correct
13 Correct 11 ms 760 KB Output is correct
14 Correct 10 ms 632 KB Output is correct
15 Correct 10 ms 632 KB Output is correct
16 Correct 10 ms 504 KB Output is correct
17 Correct 10 ms 632 KB Output is correct
18 Correct 10 ms 632 KB Output is correct
19 Correct 10 ms 504 KB Output is correct
20 Correct 12 ms 760 KB Output is correct
21 Correct 11 ms 632 KB Output is correct
22 Correct 11 ms 504 KB Output is correct
23 Correct 11 ms 504 KB Output is correct
24 Correct 11 ms 504 KB Output is correct
25 Correct 11 ms 504 KB Output is correct
26 Correct 10 ms 504 KB Output is correct
27 Correct 11 ms 632 KB Output is correct
28 Correct 11 ms 664 KB Output is correct
29 Correct 11 ms 632 KB Output is correct
30 Correct 10 ms 504 KB Output is correct
31 Correct 11 ms 760 KB Output is correct
32 Correct 11 ms 632 KB Output is correct
33 Correct 666 ms 10440 KB Output is correct
34 Correct 749 ms 27000 KB Output is correct
35 Correct 150 ms 4856 KB Output is correct
36 Correct 1184 ms 18168 KB Output is correct
37 Correct 1299 ms 46784 KB Output is correct
38 Correct 1295 ms 32228 KB Output is correct
39 Correct 1100 ms 18328 KB Output is correct
40 Correct 1301 ms 21156 KB Output is correct
41 Correct 1168 ms 19936 KB Output is correct
42 Correct 1158 ms 19884 KB Output is correct
43 Correct 950 ms 21804 KB Output is correct
44 Correct 1300 ms 35960 KB Output is correct
45 Correct 1136 ms 32372 KB Output is correct
46 Correct 1201 ms 31624 KB Output is correct
47 Correct 930 ms 21756 KB Output is correct
48 Correct 1218 ms 38512 KB Output is correct
49 Correct 1230 ms 32804 KB Output is correct
50 Correct 949 ms 15480 KB Output is correct
51 Correct 1358 ms 47224 KB Output is correct
52 Correct 182 ms 5752 KB Output is correct
53 Correct 1227 ms 19620 KB Output is correct
54 Correct 1386 ms 49472 KB Output is correct
55 Correct 1375 ms 34236 KB Output is correct
56 Correct 1153 ms 19776 KB Output is correct
57 Correct 1291 ms 25720 KB Output is correct
58 Correct 1228 ms 23928 KB Output is correct
59 Correct 1240 ms 21836 KB Output is correct
60 Correct 1107 ms 21336 KB Output is correct
61 Correct 1361 ms 43280 KB Output is correct
62 Correct 1332 ms 31936 KB Output is correct
63 Correct 1393 ms 32664 KB Output is correct
64 Correct 963 ms 23628 KB Output is correct
65 Correct 1245 ms 40412 KB Output is correct
66 Correct 1343 ms 34376 KB Output is correct
67 Correct 132 ms 2860 KB Output is correct
68 Correct 493 ms 21180 KB Output is correct
69 Correct 706 ms 20768 KB Output is correct
70 Correct 1148 ms 18040 KB Output is correct
71 Correct 1299 ms 46968 KB Output is correct
72 Correct 1278 ms 32192 KB Output is correct
73 Correct 1172 ms 18244 KB Output is correct
74 Correct 1225 ms 23612 KB Output is correct
75 Correct 1206 ms 20624 KB Output is correct
76 Correct 1194 ms 20332 KB Output is correct
77 Correct 1045 ms 19800 KB Output is correct
78 Correct 1323 ms 38920 KB Output is correct
79 Correct 1201 ms 35740 KB Output is correct
80 Correct 1277 ms 29872 KB Output is correct
81 Correct 873 ms 21448 KB Output is correct
82 Correct 1221 ms 38512 KB Output is correct
83 Correct 1242 ms 32868 KB Output is correct
84 Correct 1148 ms 18296 KB Output is correct
85 Correct 1309 ms 47436 KB Output is correct
86 Correct 1280 ms 32572 KB Output is correct
87 Correct 1139 ms 18384 KB Output is correct
88 Correct 1199 ms 24512 KB Output is correct
89 Correct 1199 ms 21544 KB Output is correct
90 Correct 1370 ms 20572 KB Output is correct
91 Correct 1083 ms 20128 KB Output is correct
92 Correct 1361 ms 46584 KB Output is correct
93 Correct 1224 ms 28368 KB Output is correct
94 Correct 1227 ms 25388 KB Output is correct
95 Correct 926 ms 22084 KB Output is correct
96 Correct 1205 ms 39056 KB Output is correct
97 Correct 1312 ms 33232 KB Output is correct
98 Correct 1221 ms 19676 KB Output is correct
99 Correct 1368 ms 47940 KB Output is correct
100 Correct 1309 ms 33864 KB Output is correct
101 Correct 1250 ms 19444 KB Output is correct
102 Correct 1302 ms 23892 KB Output is correct
103 Correct 1224 ms 22036 KB Output is correct
104 Correct 1281 ms 21148 KB Output is correct
105 Correct 1024 ms 20672 KB Output is correct
106 Correct 1321 ms 35584 KB Output is correct
107 Correct 1202 ms 35884 KB Output is correct
108 Correct 1261 ms 28140 KB Output is correct
109 Correct 982 ms 23012 KB Output is correct
110 Correct 1210 ms 39516 KB Output is correct
111 Correct 1280 ms 33912 KB Output is correct