QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#318285#5017. 相等树链yllcm20 3078ms139516kbC++144.4kb2024-01-30 22:11:412024-01-30 22:11:42

Judging History

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

  • [2024-01-30 22:11:42]
  • 评测
  • 测评结果:20
  • 用时:3078ms
  • 内存:139516kb
  • [2024-01-30 22:11:41]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pb push_back
#define pii pair<int, int>
#define FR first
#define SE second
using namespace std;
inline int read() {
  int x = 0; bool op = 0;
  char c = getchar();
  while(!isdigit(c))op |= (c == '-'), c = getchar();
  while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return op ? -x : x;
}
const int N = 2e5 + 10;
int n, S, tim;
ll ans;
ull val[N];
mt19937_64 rnd(114514);
vector<int> G1[N], G2[N];
int nxt[N], vis[N], id[N];
unordered_map<ull, int> cnt[3];
struct Node {int x, y;};
int st[N][25], dep[N], dfn[N];
void dfs(int u, int fa) {
  st[dfn[u] = ++tim][0] = fa; dep[u] = dep[fa] + 1;
  for(int v : G1[u])if(v != fa)dfs(v, u);
  return ;
}
int cmin(int x, int y) {return dep[x] < dep[y] ? x : y;}
void build() {
  for(int j = 1; (1 << j) <= n; j++) {
    for(int i = 1; i + (1 << j) - 1 <= n; i++) {
      st[i][j] = cmin(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
    }
  }
  return ;
}
int lca(int u, int v) {
  if(u == v)return u;
  int l = dfn[u], r = dfn[v];
  if(l > r)swap(l, r);
  int t = __lg(r - (l++));
  return cmin(st[l][t], st[r - (1 << t) + 1][t]);
}
int dist(int u, int v) {return dep[u] + dep[v] - 2 * dep[lca(u, v)];}
Node merge(Node x, int u) {
  if(!~x.x && !~x.y)return (Node){-1, -1};
  Node now = x;
  if(dist(x.x, u) > dist(now.x, now.y))now = (Node){x.x, u};
  if(dist(x.y, u) > dist(now.x, now.y))now = (Node){x.y, u};
  vector<int> p = {x.x, x.y, u};
  auto chk = [&](Node x, int u) {return dist(x.x, u) + dist(x.y, u) == dist(x.x, x.y);};
  for(int x : p)if(!chk(now, x))return (Node){-1, -1};
  return now;
}
void dfs2(int u, int fa, ull now, Node p, int op) {
  now ^= val[u]; now ^= val[nxt[u]];
  p = merge(p, u);
  if(~p.x && ~p.y) {
    if(!op) {
      auto add = [&](ull now, int c) {
        for(int i = 0; i + c <= 2; i++)ans += cnt[i][now];
        return ;
      };
      add(now, 0);
      add(now ^ val[p.x], 1);
      // ans += cnt[2][now] + cnt[1][now];
      // ans += cnt[1][now ^ val[p.x]] + cnt[0][now ^ val[p.x]];
      if(p.x != p.y) {
        add(now ^ val[p.y], 1);
        add(now ^ val[p.x] ^ val[p.y], 2);
        // ans += cnt[1][now ^ val[p.y]];
        // ans += cnt[now ^ val[p.x] ^ val[p.y]];
      }
      // printf("qwq:%d %d %d %d\n", u, p.x, p.y, ans);
    }
    else {
      cnt[0][now]++;
      cnt[1][now ^ val[p.x]]++;
      if(p.x != p.y) {
        cnt[1][now ^ val[p.y]]++;
        cnt[2][now ^ val[p.x] ^ val[p.y]]++;
      }
    }
  }
  for(int v : G2[u])if(v != fa && id[v] == tim)dfs2(v, u, now, p, op);
  return ;
}
void calc(int u) {
  for(int i = 0; i <= 2; i++) {
    unordered_map<ull, int>().swap(cnt[i]);
  }
  cnt[0][0] = 1;
  for(int v : G2[u]) {
    // cout << "calc:" << u << ' ' << v << endl;
    if(id[v] != tim)continue;
    dfs2(v, u, 0, (Node){v, v}, 0); 
    dfs2(v, u, 0, (Node){v, v}, 1);
  }
  return ;
}
int root;
int sz[N];
void findroot(int u, int fa) {
  int mx = 0; sz[u] = 1;
  for(int v : G1[u]) {
    if(v == fa || vis[v])continue;
    findroot(v, u); sz[u] += sz[v];
    mx = max(mx, sz[v]);
  }
  if(max(S - sz[u], mx) * 2 <= S)root = u;
  return ;
}
void dfs1(int u, int fa) {
  id[u] = tim; nxt[u] = fa;
  for(int v : G1[u]) {
    if(v == fa || vis[v])continue;
    dfs1(v, u);
  }
  return ;
}
void solve(int u) {
  // if(S > 0)cerr << u << ' ' << S << endl;
  // cerr << "solve:" << u << ' ' << root << ' ' << vis[root] << ' ' << S << endl;
  findroot(u, 0); u = root;

  vis[u] = true;
  tim++; 
  for(int v : G1[u])if(!vis[v])dfs1(v, 0); 
  // printf("solve:%d\n", u);
  // for(int i = 1; i <= n; i++)cout << id[i] << ' '; cout << endl;
  calc(u);
  // printf("%d %d\n", u, ans);
  int nS = S;
  for(int v : G1[u]) {
    if(vis[v])continue;
    S = sz[v] > sz[u] ? nS - sz[u] : sz[v];
    // cerr << u << ' ' << v << endl;
    solve(v);
  }
  return ;
}
int main() { 
  n = read();
  for(int i = 1; i <= n; i++)val[i] = rnd();
  for(int i = 2; i <= n; i++) {
    int u = read();
    G1[u].pb(i); G1[i].pb(u);
  }
  for(int i = 2; i <= n; i++) {
    int u = read();
    // printf("%d %d\n", u, i);
    G2[u].pb(i); G2[i].pb(u);
  }
  // for(int x : G2[2])cout << x << endl;
  dfs(1, 0); build();
  S = ans = n; solve(1);
  printf("%lld\n", ans);
  return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 11ms
memory: 21804kb

input:

5000
1296 1400 867 4533 1296 2007 2059 115 821 2960 3187 1597 2409 2708 4743 4778 1345 3967 911 3400 4249 3793 339 3145 3490 607 4148 3513 3264 3852 568 775 828 1348 423 3678 305 1938 1096 1373 2662 1048 4328 4208 203 779 3103 3372 4523 192 264 792 4943 2211 2494 3513 3555 4935 3277 3390 4624 128 18...

output:

76285

result:

wrong answer 1st numbers differ - expected: '76002', found: '76285'

Subtask #2:

score: 20
Accepted

Test #6:

score: 20
Accepted
time: 1479ms
memory: 139516kb

input:

200000
13177 40498 104798 83659 186055 32788 86489 72123 13521 134868 158968 60124 166316 163390 120935 103000 83938 57807 97940 40447 137723 154683 191864 59080 102808 3969 21451 165592 128776 49468 4101 26441 139317 59503 18053 118809 187783 149816 21609 98521 165692 52964 60425 23437 29614 106886...

output:

5859368

result:

ok 1 number(s): "5859368"

Test #7:

score: 0
Accepted
time: 1883ms
memory: 130056kb

input:

200000
161252 109349 161307 131176 54282 35989 53345 116328 52886 20845 166068 198634 185607 110703 32172 153437 50060 194586 193712 73284 32556 105087 55275 157714 22357 182552 31342 100889 145473 91759 18280 144489 108133 130988 11561 20028 121278 138065 83647 33848 33634 31990 198971 110781 12801...

output:

110388948

result:

ok 1 number(s): "110388948"

Test #8:

score: 0
Accepted
time: 2352ms
memory: 126108kb

input:

200000
36915 117643 88659 78272 142053 101722 71138 149291 152470 118051 45210 31645 187500 22733 178345 55170 28768 44890 26946 76823 76271 9886 197447 130337 74747 175940 118067 191159 19884 113644 73935 160371 97288 153196 50668 47567 113533 73075 90904 115114 191694 127924 127338 41621 134964 59...

output:

469103910

result:

ok 1 number(s): "469103910"

Test #9:

score: 0
Accepted
time: 2156ms
memory: 128172kb

input:

200000
3943 160214 22824 98337 873 3550 102218 67841 56961 130137 87920 154401 45794 144615 52487 75188 13477 151928 41794 147148 88519 25499 59155 187395 70572 37799 7846 166650 165689 178923 110784 68004 124416 7070 37566 126445 23236 78630 190578 145179 81517 809 99830 98383 67869 158370 182186 1...

output:

254891707

result:

ok 1 number(s): "254891707"

Test #10:

score: 0
Accepted
time: 2542ms
memory: 127804kb

input:

200000
78377 9603 105868 5816 97565 17017 11229 64332 152282 115911 5141 119594 138303 67697 62645 28928 113832 166252 170769 60777 39110 85804 122988 117490 80461 169830 15334 189378 9037 191383 143689 123124 18788 113025 35138 63649 116803 33050 135937 99323 119570 44477 174794 28051 74975 174331 ...

output:

779879990

result:

ok 1 number(s): "779879990"

Test #11:

score: 0
Accepted
time: 3078ms
memory: 122084kb

input:

200000
31706 198038 102731 72443 44408 116386 129202 193795 176464 175136 12293 17325 194955 2759 172903 37032 60623 73343 55344 138068 10675 29053 29280 94350 175071 73192 10795 127030 18516 28564 170635 88693 143311 110487 10208 57489 1052 33420 156977 149595 34056 171577 39262 71741 71633 61355 1...

output:

4797642624

result:

ok 1 number(s): "4797642624"

Subtask #3:

score: 0
Wrong Answer

Dependency #2:

100%
Accepted

Test #12:

score: 0
Wrong Answer
time: 925ms
memory: 90016kb

input:

200000
62936 42114 49454 95737 154735 83651 12241 12518 111465 87130 38023 12482 194231 193238 50051 69033 102675 40262 72917 146819 56538 159148 35426 119935 46694 63476 37721 177034 120832 10487 177187 12093 118464 95232 28721 165669 13308 116990 16648 187886 3227 181605 10993 174426 186874 45794 ...

output:

4854362

result:

wrong answer 1st numbers differ - expected: '4835011', found: '4854362'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%