QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#768629#2766. Unique Cities_8_8_0 335ms220028kbC++174.3kb2024-11-21 13:04:042024-11-21 13:04:09

Judging History

你现在查看的是最新测评结果

  • [2024-11-21 13:04:09]
  • 评测
  • 测评结果:0
  • 用时:335ms
  • 内存:220028kb
  • [2024-11-21 13:04:04]
  • 提交

answer

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

const int  N = 2e5 + 12, MOD = (int)1e9 + 7;

int n, m, a[N];
bool vis[N], timer;
vector<int> g[N], gt[N];
vector<int> dist[N];
int dd = 0;
bool zap;
int f(int d) {
    vector<int> D(n + 1, 0);
    timer = 1;
    for(int i = 1; i <= n; i++) {
        vis[i] = 0;
    }
    vis[d] = timer;
    int lst = d;
    queue<int> q;
    q.push(d);
    while(!q.empty()) {
        int v = q.front();
        lst = v;
        q.pop();
        for(int to : g[v]) {
            if(vis[to] != timer) {
                D[to] = D[v] + 1;
                q.push(to);
                vis[to] = timer;
            }
        }
    }
    if(dd) {
        dist[dd].resize(n + 1);
        for(int i = 1; i <= n; i++) {
            dist[dd][i] = D[i];    
        }
    }
    dd++;
    return lst;
}
int dep[N], mxd[N], res[N], d, d1, o, cr;
void dfs(int v, int pr = -1) {
    mxd[v] = dep[v];
    for(int to:g[v]) if(to != pr) {
        gt[v].push_back(to);
        dep[to] = dep[v] + 1;
        dfs(to, v);
        mxd[v] = max(mxd[v], mxd[to]);
    }
    for(int i = 0; i < (int)gt[v].size(); i++) {
        if(mxd[gt[v][i]] == mxd[v]) {
            swap(gt[v][0], gt[v][i]);
        }
    }
}
int b = 17, it = 0, cur = 0, ver[N];
int up[N][18];
struct node{
    node *l = 0, *r = 0;
    int sum = 0;
    node(){};
    node(int v) {
        sum = v;
    }
    node (node *L, node *R) {
        l = L;
        r = R;
        sum = l->sum + r->sum;
    }
};
using pnode = node *;
pnode tr[N];    
pnode build(int tl = 1, int tr = m) {
    if(tl == tr) {
        return new node();
    }
    int tm = (tl + tr) >> 1;
    return new node(build(tl, tm), build(tm + 1, tr));
}
pnode upd(int pos, pnode v, int tl = 1, int tr = m) {
    if(tl == tr) {
        return new node(1);
    }
    int tm = (tl + tr) >> 1;
    if(pos <= tm) 
        return new node(upd(pos, v->l, tl, tm), v->r);
    return new node(v->l, upd(pos, v->r, tm + 1, tr));
}
void go(int v) {
    if(dist[cr][v] > dist[o][v] || (dist[cr][v] == dist[o][v] && zap)) {
        int f = cur, val = -mxd[v] + dep[v] * 2;
        set<int> r;
        if(dep[ver[f]] >= val) {
            for(int i = b - 1; i >= 0; i--) {
                int nv = ver[up[f][i]];
                if(dep[nv] >= val) {
                    f = up[f][i];
                }
            }
            f = up[f][0];
        }
        res[v] += tr[f]->sum;
    }
    if(gt[v].empty()) return;
    int sz = (int)gt[v].size();
    int mx1 = dep[v];
    for(int i = 1; i < sz; i++) {
        mx1 = max(mx1, mxd[gt[v][i]]); 
    }
    int bf = cur;
    for(int i = 0; i < sz; i++) {
        int to = gt[v][i];
        int mx = dep[v];
        if(i) mx = max(mx, mxd[gt[v][0]]);
        if(i < sz - 1) mx = mx1;
        mx -= dep[v];
        if(i == 1) {
            cur = up[cur][0];
        }
        if(i <= 1 && dep[v] - dep[ver[cur]] <= mx) {
            for(int i = b - 1; i >= 0; i--) {
                int nv = up[cur][i];
                if(dep[v] - dep[ver[nv]] <= mx) {
                    cur = nv;
                }
            }
            cur = up[cur][0];
        }
        if(i <= 1) {
            it++;
            ver[it] = v;
            up[it][0] = cur;
            tr[it] = upd(a[v], tr[cur]);
            for(int i = 1; i < b; i++) {
                up[it][i] = up[up[it][i - 1]][i - 1];
            }
            cur = it;
        }
        go(to);
    }
    cur = bf;
}
void solve(int root) {
    it = cur = 0;
    for(int i = 1; i <= n; i++) {
        gt[i].clear();
    }
    ver[0] = 0;
    dep[0] = -(int)1e9;
    tr[0] = build();
    dep[root] = 1;
    dfs(root);

    go(root);

}
void test() {
    cin >> n >> m;
    for(int i = 1; i <= n - 1; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    d = f(1), d1 = f(d);
    f(d1);
    o = 2;cr = 1;
    solve(d);
    zap = 1;
    o = 1;cr = 2;
    solve(d1);
    for(int i = 1; i <= n; i++) {
        cout << res[i] << '\n';
    }

}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t = 1; 
    // cin >> t;

    while(t--) 
        test();
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 4
Accepted
time: 0ms
memory: 22508kb

input:

2 1
1 2
1 1

output:

1
1

result:

ok 2 lines

Test #2:

score: 0
Wrong Answer
time: 3ms
memory: 25864kb

input:

1842 848
740 1093
1299 922
801 1560
265 1664
1043 65
1430 427
80 233
4 1238
1623 1473
1569 274
953 1485
1259 649
1671 1409
246 542
742 1517
720 1120
1527 1328
1167 1531
1056 1130
673 1222
192 980
1393 913
446 688
135 23
1065 1787
978 1481
1765 1720
310 202
1406 1451
475 523
104 774
1531 829
169 396
...

output:

1
1
1
0
0
0
2
0
3
0
2
1
0
3
0
0
0
2
0
0
4
2
0
2
4
0
0
1
1
1
2
1
1
1
0
0
1
3
0
1
2
0
1
1
4
0
1
1
1
3
0
2
1
0
1
1
0
0
2
1
1
2
1
1
1
0
3
0
1
2
0
0
1
0
0
0
1
1
3
1
4
3
1
2
2
0
1
3
0
2
1
0
1
1
1
1
1
2
3
1
0
1
2
1
0
1
2
4
1
3
0
0
0
4
0
1
1
2
2
2
3
0
5
2
1
5
0
1
1
0
0
2
0
2
0
2
4
0
1
0
3
0
3
0
3
2
0
1
0
2
...

result:

wrong answer 145th lines differ - expected: '0', found: '3'

Subtask #2:

score: 0
Wrong Answer

Test #33:

score: 0
Wrong Answer
time: 93ms
memory: 45076kb

input:

115391 1
32067 50006
1710 5850
21016 66364
72998 34367
24783 10670
49950 93666
81835 81234
53480 68963
87357 43320
93905 30509
72528 92224
520 100511
54804 2917
58490 23858
93643 87530
90737 65205
60740 110812
9553 90266
70028 67222
108045 88982
35584 110286
53518 21733
108735 26404
108228 109796
92...

output:

1
1
0
1
0
1
1
0
1
1
1
1
1
1
1
1
1
0
1
0
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
0
0
1
1
0
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
0
1
1
0
1
1
0
1
1
1
1
1
1
1
1
0
1
0
1
1
0
1
1
1
1
1
0
1
1
1
1
0
1
1
0
1
0
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

wrong answer 158th lines differ - expected: '0', found: '1'

Subtask #3:

score: 0
Wrong Answer

Test #50:

score: 0
Wrong Answer
time: 335ms
memory: 220028kb

input:

157976 157976
20171 157173
44732 54119
107845 121149
109200 110309
82678 108206
89140 64200
36916 128759
3966 123760
92978 105716
43700 146126
14924 3429
107721 36095
94906 78173
97162 29552
119574 39605
25145 138492
16622 99431
60281 7949
76176 136644
75716 91518
127987 110605
77999 110960
101187 5...

output:

1
5
3
4
4
2
3
2
2
1
2
1
4
1
3
1
7
2
1
3
1
5
2
1
1
2
1
3
1
4
2
1
1
1
1
1
3
1
2
5
4
6
3
2
1
1
2
3
4
1
1
1
1
4
2
4
3
2
2
1
1
4
2
4
1
3
1
2
1
1
1
1
1
3
2
1
1
5
2
1
1
9
2
5
1
1
3
1
2
4
2
2
2
3
1
4
2
1
1
1
2
1
3
1
4
1
6
2
1
2
3
2
1
1
1
2
1
1
2
2
1
2
8
2
2
1
2
1
1
5
2
2
2
1
3
1
4
1
1
4
3
1
10
6
3
1
3
2
2
9...

result:

wrong answer 4th lines differ - expected: '3', found: '4'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%