QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#309428#8134. LCA Countingucup-team266#WA 2ms22092kbC++232.1kb2024-01-20 17:17:302024-01-20 17:17:30

Judging History

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

  • [2024-01-20 17:17:30]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:22092kb
  • [2024-01-20 17:17:30]
  • 提交

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
void die(string S){puts(S.c_str());exit(0);}
int n;
int anc[20][200200];
int depth[200200];
int lca(int u,int v)
{
	for(int i=19;i>=0;i--)
	{
		if(depth[anc[i][u]]>=depth[v])
			u=anc[i][u];
		if(depth[anc[i][v]]>=depth[u])
			v=anc[i][v];
	}
	if(u==v) return u;
	for(int i=19;i>=0;i--)
		if(anc[i][u]!=anc[i][v])
		{
			u=anc[i][u];
			v=anc[i][v];
		}
	return anc[0][u];
}
bool isleaf[200200];
int dfn[200200],tot;
vector<int> G[200200];
void dfs(int u)
{
	dfn[u]=++tot;
	for(auto v:G[u])
		dfs(v);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=2;i<=n;i++)
		cin>>anc[0][i];
	for(int i=1;i<20;i++)
		for(int j=1;j<=n;j++)
			anc[i][j]=anc[i-1][anc[i-1][j]];
	for(int i=1;i<=n;i++)
	{
		G[anc[0][i]].pb(i);
		isleaf[anc[0][i]]=true;
		depth[i]=depth[anc[0][i]]+1;
	}
	dfs(1);
	vector<int> vec;
	for(int i=1;i<=n;i++)
		if(!isleaf[i])
			vec.pb(i);
	sort(ALL(vec),[&](const int &a,const int &b){return dfn[a]<dfn[b];});
	int ans=0;
	vector<int> st;
	for(auto x:vec)
	{
		if(!sz(st))
		{
			st.pb(x);
			continue;
		}
		int a=lca(x,st.back());
		while(sz(st)>1&&depth[a]<depth[st[sz(st)-2]])
		{
			ans++;
			st.pop_back();
		}
		if(depth[a]<depth[st.back()])
		{
			ans++;
			st.pop_back();
		}
		if(!sz(st)||st.back()!=a)
			st.pb(a);
		st.pb(x);
	}
	ans+=sz(st);
	ans-=sz(vec);
	int val=-1;
	ans++;
	for(int i=0;i<sz(vec);i++)
	{
		val++;
		if(ans>0) val++;
		ans--;
		cout<<val<<" ";
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 22092kb

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: 2ms
memory: 20244kb

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: 18224kb

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: 19956kb

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: -100
Wrong Answer
time: 0ms
memory: 20052kb

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 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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:

wrong answer 5th numbers differ - expected: '8', found: '9'