Submission #98991

# Submission time Handle Problem Language Result Execution time Memory
98991 2019-02-28T00:55:21 Z Benq Unique Cities (JOI19_ho_t5) C++14
100 / 100
1041 ms 56520 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 200001;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

namespace input {
    template<class T> void re(complex<T>& x);
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);

    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    void re(ld& x) { string t; re(t); x = stold(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) { 
        re(first); re(rest...); 
    }

    template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}

using namespace input;

namespace output {
    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) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... 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 prContain(const T& x) {
        pr("{");
        bool fst = 1; trav(a,x) pr(!fst?", ":"",a), fst = 0; 
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
    
    template<class Arg> void ps(const Arg& first) { pr(first,"\n"); } // print w/ spaces
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { 
        pr(first," "); ps(rest...); 
    }
}

using namespace output;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

namespace modOp {
    int ad(int a, int b, int mod = MOD) { return (a+b)%mod; }
    int sub(int a, int b, int mod = MOD) { return (a-b+mod)%mod; }
    int mul(int a, int b, int mod = MOD) { return (ll)a*b%mod; }
    
    int AD(int& a, int b, int mod = MOD) { return a = ad(a,b,mod); }
    int SUB(int& a, int b, int mod = MOD) { return a = sub(a,b,mod); }
    int MUL(int& a, int b, int mod = MOD) { return a = mul(a,b,mod); }
    
    int po (int b, int p, int mod = MOD) { return !p?1:mul(po(mul(b,b,mod),p/2,mod),p&1?b:1,mod); }
    int inv (int b, int mod = MOD) { return po(b,mod-2,mod); }
    
    int invGeneral(int a, int b) { // 0 < a < b, gcd(a,b) = 1
        if (a == 0) return b == 1 ? 0 : -1;
        int x = invGeneral(b%a,a); 
        return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
    }
}

using namespace modOp;

template<int SZ> struct TreeDiameter {
    int n, dist[SZ], pre[SZ];
    vi adj[SZ];

    void addEdge(int a, int b) {
        adj[a].pb(b), adj[b].pb(a);
    }

    void dfs(int cur) {
        for (int i: adj[cur]) if (i != pre[cur]) {
            pre[i] = cur;
            dist[i] = dist[cur]+1;
            dfs(i);
        }
    }

    void genDist(int cur) {
        memset(dist,0,sizeof dist);
        pre[cur] = -1;
        dfs(cur);
    }

    vi diameter() {
        vi res;
        genDist(1);
        int bes = 0; FOR(i,1,n+1) if (dist[i] > dist[bes]) bes = i;
        res.pb(bes);
        genDist(bes); FOR(i,1,n+1) if (dist[i] > dist[bes]) bes = i;
        res.pb(bes);
        return res;
    }
};


TreeDiameter<MX> T;

int M,CUR,co[MX],c[MX],len[MX];
pi ans[MX][2];
vpi seq;

void modi(int a, int b) {
    a = c[a];
    if (co[a]) CUR --;
    co[a] += b;
    if (co[a]) CUR ++;
}

void solve(int ind, int cur, int dist, int pre) {
    pi maxSub = {-1,-1};
    trav(a,T.adj[cur]) if (a != pre) ckmax(maxSub,{len[a],a});
    int l = 0; trav(a,T.adj[cur]) if (a != pre && a != maxSub.s) ckmax(l,len[a]+1); 
    
    vpi del;
    while (sz(seq) && seq.back().f >= dist-l) {
        del.pb(seq.back());
        modi(seq.back().s,-1);
        seq.pop_back();
    }
    seq.pb({dist,cur}); modi(seq.back().s,1);
    if (maxSub.s != -1) solve(ind,maxSub.s,dist+1,cur);
    modi(seq.back().s,-1);
    seq.pop_back();
    while (sz(seq) && seq.back().f >= dist-(maxSub.f+1)) {
        del.pb(seq.back());
        modi(seq.back().s,-1);
        seq.pop_back();
    }
    ans[cur][ind] = {dist,CUR};
    seq.pb({dist,cur}); modi(seq.back().s,1);
    trav(a,T.adj[cur]) if (a != pre && a != maxSub.s) solve(ind,a,dist+1,cur);
    modi(seq.back().s,-1); seq.pop_back();
    if (pre != -1 && (!sz(seq) || seq.back().s != pre)) {
        seq.pb({dist-1,pre}); modi(seq.back().s,1);
    }
    /*reverse(all(del)); 
    trav(a,del) {
        seq.pb(a);
        modi(seq.back().s,1);
    }*/
}

void genLen(int x, int y) {
    len[x] = 0;
    trav(i,T.adj[x]) if (i != y) {
        genLen(i,x);
        ckmax(len[x],len[i]+1);
    }
}

int main() {
    // you should actually read the stuff at the bottom
    setIO(); re(T.n,M);
    F0R(i,T.n-1) {
        int a,b; re(a,b);
        T.addEdge(a,b);
    }
    FOR(i,1,T.n+1) re(c[i]);
    auto a = T.diameter();
    
    //ps("WHAT",a);
    
    genLen(a[0],-1);
    solve(0,a[0],0,-1);
    // ps("HUH",sz(seq));
    genLen(a[1],-1);
    solve(1,a[1],0,-1);
    //FOR(i,1,T.n+1) ps("HUH",ans[i][0],ans[i][1]);
    FOR(i,1,T.n+1) ps(max(ans[i][0],ans[i][1]).s);
    // you should actually read the stuff at the bottom
}

/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do smth instead of nothing and stay organized
*/

Compilation message

joi2019_ho_t5.cpp: In function 'void io::setIn(std::__cxx11::string)':
joi2019_ho_t5.cpp:112:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     void setIn(string s) { freopen(s.c_str(),"r",stdin); }
                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'void io::setOut(std::__cxx11::string)':
joi2019_ho_t5.cpp:113:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     void setOut(string s) { freopen(s.c_str(),"w",stdout); }
                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5888 KB Output is correct
2 Correct 10 ms 6016 KB Output is correct
3 Correct 10 ms 6016 KB Output is correct
4 Correct 10 ms 6144 KB Output is correct
5 Correct 9 ms 6016 KB Output is correct
6 Correct 10 ms 6272 KB Output is correct
7 Correct 10 ms 6116 KB Output is correct
8 Correct 8 ms 6016 KB Output is correct
9 Correct 9 ms 6016 KB Output is correct
10 Correct 9 ms 6016 KB Output is correct
11 Correct 9 ms 6016 KB Output is correct
12 Correct 9 ms 6016 KB Output is correct
13 Correct 9 ms 6272 KB Output is correct
14 Correct 8 ms 6144 KB Output is correct
15 Correct 11 ms 6016 KB Output is correct
16 Correct 8 ms 6016 KB Output is correct
17 Correct 8 ms 6144 KB Output is correct
18 Correct 11 ms 6144 KB Output is correct
19 Correct 11 ms 6016 KB Output is correct
20 Correct 12 ms 6400 KB Output is correct
21 Correct 9 ms 6144 KB Output is correct
22 Correct 8 ms 6016 KB Output is correct
23 Correct 9 ms 6016 KB Output is correct
24 Correct 10 ms 6016 KB Output is correct
25 Correct 9 ms 6016 KB Output is correct
26 Correct 8 ms 6016 KB Output is correct
27 Correct 9 ms 6244 KB Output is correct
28 Correct 8 ms 6272 KB Output is correct
29 Correct 11 ms 6144 KB Output is correct
30 Correct 7 ms 6016 KB Output is correct
31 Correct 9 ms 6144 KB Output is correct
32 Correct 9 ms 6144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 330 ms 12992 KB Output is correct
2 Correct 424 ms 31844 KB Output is correct
3 Correct 47 ms 9976 KB Output is correct
4 Correct 607 ms 18168 KB Output is correct
5 Correct 914 ms 51100 KB Output is correct
6 Correct 805 ms 34780 KB Output is correct
7 Correct 624 ms 18164 KB Output is correct
8 Correct 666 ms 21892 KB Output is correct
9 Correct 606 ms 20600 KB Output is correct
10 Correct 691 ms 20472 KB Output is correct
11 Correct 337 ms 18752 KB Output is correct
12 Correct 758 ms 39756 KB Output is correct
13 Correct 678 ms 34640 KB Output is correct
14 Correct 754 ms 33552 KB Output is correct
15 Correct 421 ms 18796 KB Output is correct
16 Correct 762 ms 40296 KB Output is correct
17 Correct 699 ms 35044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 518 ms 16208 KB Output is correct
2 Correct 1034 ms 50688 KB Output is correct
3 Correct 87 ms 11192 KB Output is correct
4 Correct 684 ms 22716 KB Output is correct
5 Correct 1041 ms 56520 KB Output is correct
6 Correct 972 ms 39356 KB Output is correct
7 Correct 666 ms 22824 KB Output is correct
8 Correct 744 ms 30212 KB Output is correct
9 Correct 713 ms 27512 KB Output is correct
10 Correct 754 ms 25676 KB Output is correct
11 Correct 587 ms 23084 KB Output is correct
12 Correct 996 ms 50280 KB Output is correct
13 Correct 770 ms 37744 KB Output is correct
14 Correct 962 ms 37356 KB Output is correct
15 Correct 357 ms 23528 KB Output is correct
16 Correct 933 ms 45648 KB Output is correct
17 Correct 913 ms 39528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5888 KB Output is correct
2 Correct 10 ms 6016 KB Output is correct
3 Correct 10 ms 6016 KB Output is correct
4 Correct 10 ms 6144 KB Output is correct
5 Correct 9 ms 6016 KB Output is correct
6 Correct 10 ms 6272 KB Output is correct
7 Correct 10 ms 6116 KB Output is correct
8 Correct 8 ms 6016 KB Output is correct
9 Correct 9 ms 6016 KB Output is correct
10 Correct 9 ms 6016 KB Output is correct
11 Correct 9 ms 6016 KB Output is correct
12 Correct 9 ms 6016 KB Output is correct
13 Correct 9 ms 6272 KB Output is correct
14 Correct 8 ms 6144 KB Output is correct
15 Correct 11 ms 6016 KB Output is correct
16 Correct 8 ms 6016 KB Output is correct
17 Correct 8 ms 6144 KB Output is correct
18 Correct 11 ms 6144 KB Output is correct
19 Correct 11 ms 6016 KB Output is correct
20 Correct 12 ms 6400 KB Output is correct
21 Correct 9 ms 6144 KB Output is correct
22 Correct 8 ms 6016 KB Output is correct
23 Correct 9 ms 6016 KB Output is correct
24 Correct 10 ms 6016 KB Output is correct
25 Correct 9 ms 6016 KB Output is correct
26 Correct 8 ms 6016 KB Output is correct
27 Correct 9 ms 6244 KB Output is correct
28 Correct 8 ms 6272 KB Output is correct
29 Correct 11 ms 6144 KB Output is correct
30 Correct 7 ms 6016 KB Output is correct
31 Correct 9 ms 6144 KB Output is correct
32 Correct 9 ms 6144 KB Output is correct
33 Correct 330 ms 12992 KB Output is correct
34 Correct 424 ms 31844 KB Output is correct
35 Correct 47 ms 9976 KB Output is correct
36 Correct 607 ms 18168 KB Output is correct
37 Correct 914 ms 51100 KB Output is correct
38 Correct 805 ms 34780 KB Output is correct
39 Correct 624 ms 18164 KB Output is correct
40 Correct 666 ms 21892 KB Output is correct
41 Correct 606 ms 20600 KB Output is correct
42 Correct 691 ms 20472 KB Output is correct
43 Correct 337 ms 18752 KB Output is correct
44 Correct 758 ms 39756 KB Output is correct
45 Correct 678 ms 34640 KB Output is correct
46 Correct 754 ms 33552 KB Output is correct
47 Correct 421 ms 18796 KB Output is correct
48 Correct 762 ms 40296 KB Output is correct
49 Correct 699 ms 35044 KB Output is correct
50 Correct 518 ms 16208 KB Output is correct
51 Correct 1034 ms 50688 KB Output is correct
52 Correct 87 ms 11192 KB Output is correct
53 Correct 684 ms 22716 KB Output is correct
54 Correct 1041 ms 56520 KB Output is correct
55 Correct 972 ms 39356 KB Output is correct
56 Correct 666 ms 22824 KB Output is correct
57 Correct 744 ms 30212 KB Output is correct
58 Correct 713 ms 27512 KB Output is correct
59 Correct 754 ms 25676 KB Output is correct
60 Correct 587 ms 23084 KB Output is correct
61 Correct 996 ms 50280 KB Output is correct
62 Correct 770 ms 37744 KB Output is correct
63 Correct 962 ms 37356 KB Output is correct
64 Correct 357 ms 23528 KB Output is correct
65 Correct 933 ms 45648 KB Output is correct
66 Correct 913 ms 39528 KB Output is correct
67 Correct 51 ms 8056 KB Output is correct
68 Correct 353 ms 27368 KB Output is correct
69 Correct 414 ms 25864 KB Output is correct
70 Correct 669 ms 21116 KB Output is correct
71 Correct 852 ms 53988 KB Output is correct
72 Correct 877 ms 37732 KB Output is correct
73 Correct 602 ms 21156 KB Output is correct
74 Correct 704 ms 27988 KB Output is correct
75 Correct 647 ms 24232 KB Output is correct
76 Correct 666 ms 23928 KB Output is correct
77 Correct 553 ms 21508 KB Output is correct
78 Correct 901 ms 45900 KB Output is correct
79 Correct 779 ms 41132 KB Output is correct
80 Correct 817 ms 34284 KB Output is correct
81 Correct 386 ms 21356 KB Output is correct
82 Correct 757 ms 43296 KB Output is correct
83 Correct 814 ms 37632 KB Output is correct
84 Correct 624 ms 21496 KB Output is correct
85 Correct 833 ms 54628 KB Output is correct
86 Correct 745 ms 37908 KB Output is correct
87 Correct 661 ms 21376 KB Output is correct
88 Correct 665 ms 28712 KB Output is correct
89 Correct 605 ms 25208 KB Output is correct
90 Correct 611 ms 24276 KB Output is correct
91 Correct 488 ms 21900 KB Output is correct
92 Correct 895 ms 53464 KB Output is correct
93 Correct 694 ms 33396 KB Output is correct
94 Correct 665 ms 30088 KB Output is correct
95 Correct 323 ms 22052 KB Output is correct
96 Correct 786 ms 43792 KB Output is correct
97 Correct 828 ms 38164 KB Output is correct
98 Correct 669 ms 22948 KB Output is correct
99 Correct 895 ms 55060 KB Output is correct
100 Correct 893 ms 39016 KB Output is correct
101 Correct 569 ms 22132 KB Output is correct
102 Correct 683 ms 27896 KB Output is correct
103 Correct 668 ms 25656 KB Output is correct
104 Correct 673 ms 24752 KB Output is correct
105 Correct 529 ms 22200 KB Output is correct
106 Correct 824 ms 41840 KB Output is correct
107 Correct 785 ms 41784 KB Output is correct
108 Correct 766 ms 33188 KB Output is correct
109 Correct 387 ms 22956 KB Output is correct
110 Correct 733 ms 44648 KB Output is correct
111 Correct 855 ms 38888 KB Output is correct