QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#310471 | #8134. LCA Counting | Kevin5307 | AC ✓ | 97ms | 24980kb | C++23 | 1.6kb | 2024-01-21 14:34:44 | 2024-01-21 14:34:44 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
#define set multiset
void die(string S){puts(S.c_str());exit(0);}
int n;
vector<int> G[200200];
set<int> st[200200];
void dfs(int u)
{
if(!sz(G[u]))
{
st[u].insert(0);
return ;
}
vector<pii> vec;
for(auto v:G[u])
{
dfs(v);
vec.pb(mp(*st[v].rbegin(),v));
}
if(sz(vec)==1)
{
swap(st[u],st[G[u][0]]);
return ;
}
rsrt(vec);
int a=vec[0].second,b=vec[1].second;
st[u].insert(vec[0].first+vec[1].first+1);
st[a].erase(prev(st[a].end()));
st[b].erase(prev(st[b].end()));
for(auto v:G[u])
{
if(sz(st[v])>sz(st[u]))
swap(st[v],st[u]);
for(auto x:st[v])
st[u].insert(x);
set<int>().swap(st[v]);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=2;i<=n;i++)
{
int p;
cin>>p;
G[p].pb(i);
}
dfs(1);
vector<int> vec;
for(auto x:st[1])
{
while(x--)
vec.pb(2);
vec.pb(1);
}
rev(vec);
int ans=0;
for(auto x:vec)
{
ans+=x;
cout<<ans<<" ";
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13400kb
input:
7 1 1 2 4 2 2
output:
1 3 5 6
result:
ok 4 number(s): "1 3 5 6"
Test #2:
score: 0
Accepted
time: 0ms
memory: 14936kb
input:
10 1 1 2 2 1 1 1 2 4
output:
1 3 5 6 7 8 9
result:
ok 7 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 14468kb
input:
10 1 2 2 4 5 6 1 2 4
output:
1 3 5 7 8
result:
ok 5 number(s): "1 3 5 7 8"
Test #4:
score: 0
Accepted
time: 0ms
memory: 13496kb
input:
10 1 2 3 3 3 5 6 4 9
output:
1 3 4
result:
ok 3 number(s): "1 3 4"
Test #5:
score: 0
Accepted
time: 0ms
memory: 13116kb
input:
1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 4 1 2 2 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 1 6 3 1 1 1 2 2 1 1 2 1 2 1 3 1 2 1 1 2 1 1 1 1 1 1 1 4 1 5 1 1 1 1 1 2 1 1 2 1 2 1 2 5 3 1 3 1 1 2 1 2 1 1 3 2 1 6 2 1 2 5 1 1 1 3 2 1 2 1 1 1 1 1...
output:
1 3 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29 31 32 34 35 37 38 40 41 43 44 46 47 49 50 52 53 55 56 58 59 61 62 64 65 67 68 70 71 73 74 76 77 79 80 82 83 85 86 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 12...
result:
ok 962 numbers
Test #6:
score: 0
Accepted
time: 2ms
memory: 15160kb
input:
1000 1 1 1 1 1 1 1 2 2 1 2 3 1 1 1 2 2 1 1 1 2 1 2 3 8 2 2 2 8 1 6 3 1 1 1 4 2 14 8 2 1 1 1 3 1 4 1 1 3 11 1 1 2 2 3 2 5 5 6 5 3 3 10 5 3 1 9 14 3 19 4 15 4 3 3 10 3 10 5 1 2 12 7 13 3 9 6 19 9 6 4 3 5 28 10 16 8 12 39 1 11 12 2 2 10 15 5 28 10 4 4 5 5 18 15 15 11 5 25 7 2 10 1 24 8 15 14 11 38 9 9 ...
output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 28 30 32 34 36 37 39 41 43 44 46 48 50 51 53 55 57 58 60 62 64 65 67 69 71 72 74 76 78 79 81 83 85 86 88 90 92 93 95 97 99 100 102 104 105 107 109 110 112 114 115 117 119 120 122 124 125 127 129 130 132 133 135 136 138 139 141 142 144 145 147 148 150 151 153 154 ...
result:
ok 822 numbers
Test #7:
score: 0
Accepted
time: 0ms
memory: 13488kb
input:
1000 1 2 2 2 1 4 3 6 7 5 8 8 1 12 12 16 4 16 18 3 17 22 7 22 9 17 21 16 16 18 29 31 12 29 32 36 22 31 32 14 15 13 8 1 36 19 33 2 10 28 29 1 53 27 15 56 34 57 21 15 58 36 57 57 16 45 16 51 44 18 31 52 53 34 65 30 51 8 59 72 53 49 49 35 31 23 3 19 69 5 16 11 45 12 62 23 46 5 24 61 63 81 1 85 73 53 84 ...
output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177...
result:
ok 487 numbers
Test #8:
score: 0
Accepted
time: 0ms
memory: 14800kb
input:
1000 1 2 3 4 5 6 7 7 8 9 11 12 13 14 14 15 17 15 17 20 19 22 22 23 24 25 22 26 23 30 29 32 32 33 33 34 30 35 39 21 41 28 42 42 44 44 38 48 49 50 51 52 44 51 44 53 36 52 59 48 55 62 63 54 65 59 54 67 60 65 68 71 71 74 58 73 65 78 76 70 77 82 80 57 85 81 86 80 79 81 78 88 88 86 61 93 88 96 98 100 96 9...
output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177...
result:
ok 374 numbers
Test #9:
score: 0
Accepted
time: 0ms
memory: 14104kb
input:
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 36 38 39 40 41 42 42 44 45 46 47 48 49 50 51 52 53 54 54 56 57 58 59 60 61 62 62 62 65 66 66 68 69 70 71 72 73 73 75 75 76 78 78 80 79 80 83 83 85 86 86 88 88 90 91 91 92 94 94 96 97 98 95 97 100 ...
output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177...
result:
ok 319 numbers
Test #10:
score: 0
Accepted
time: 82ms
memory: 24980kb
input:
200000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 2 1 1 2 1 1 1 1 1 1 1 1 3 1 2 2 1 6 1 1 2 1 3 1 1 1 1 1 2 2 1 2 1 1 1 2 5 1 2 1 1 1 1 1 1 2 3 2 1 1 1 1 1 2 1 1 2 1 2 1 2 1 1 1 3 4 2 1 3 2 2 1 1 3 1 2 1 2 4 3 2 3 1 1 2 1 5 1 3...
output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 32 34 36 38 40 42 44 46 47 49 51 53 55 57 59 61 62 64 66 68 70 72 74 75 77 79 81 83 85 86 88 90 92 94 95 97 99 101 102 104 106 108 109 111 113 115 116 118 120 122 123 125 127 129 130 132 134 136 137 139 141 143 144 146 148 150 151 153 155 157 158 160 162 16...
result:
ok 192712 numbers
Test #11:
score: 0
Accepted
time: 97ms
memory: 24872kb
input:
200000 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 4 1 1 1 1 1 7 3 1 1 1 2 2 3 3 2 3 5 2 1 1 3 1 1 1 5 2 4 1 1 2 1 8 4 11 2 6 2 3 1 11 6 1 4 5 9 5 1 1 10 1 3 4 1 24 3 1 27 4 10 9 5 2 4 5 13 5 3 1 7 4 1 9 20 17 3 16 1 2 3 3 15 2 19 5 11 3 4 14 2 11 15 4 21 9 15 3 7 9 5 3 21 2 10 7 13 26 19 7 1 16 3 14 2 9 30 16 ...
output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177...
result:
ok 165619 numbers
Test #12:
score: 0
Accepted
time: 97ms
memory: 23960kb
input:
200000 1 2 3 2 3 4 6 4 5 1 2 2 7 6 3 12 13 10 8 8 12 9 11 14 19 24 14 4 27 11 1 19 22 10 19 27 33 4 10 29 33 19 35 37 29 31 29 45 32 40 33 50 28 21 19 19 9 46 31 36 9 45 17 52 40 4 23 26 11 67 22 47 2 48 48 8 71 18 23 44 41 17 80 25 41 33 39 16 7 23 47 27 67 47 32 65 35 69 29 60 87 21 68 4 87 106 16...
output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177...
result:
ok 99943 numbers
Test #13:
score: 0
Accepted
time: 78ms
memory: 23008kb
input:
200000 1 2 3 4 4 6 6 8 8 9 11 12 13 14 15 15 17 18 19 18 16 21 22 22 24 25 21 25 26 30 28 29 32 31 33 36 36 38 37 39 31 41 42 43 42 45 44 48 41 35 43 49 50 52 51 53 57 57 57 60 60 54 62 62 64 64 63 66 68 70 61 67 69 64 75 65 76 75 63 78 79 64 77 84 81 80 81 82 83 85 76 78 86 94 94 96 97 92 86 94 92 ...
output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177...
result:
ok 76008 numbers
Test #14:
score: 0
Accepted
time: 66ms
memory: 23164kb
input:
200000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 46 48 49 50 51 52 52 54 55 55 57 58 58 59 61 62 62 64 65 65 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 83 83 85 87 88 89 88 91 92 93 94 92 94 95 98 99 100 9...
output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177...
result:
ok 73621 numbers
Extra Test:
score: 0
Extra Test Passed