QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#562868 | #6543. Visiting Friend | blueqaq | WA | 426ms | 88360kb | C++14 | 2.2kb | 2024-09-13 21:52:58 | 2024-09-13 21:53:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int T;
int n,m,q;
const int maxn=500005;
vector<int>a[maxn];
int dfn[maxn],low[maxn],tim;
int f[maxn][33];
bool v[maxn];
int siz[maxn];
int tot[maxn];
int dep[maxn];
void tarjan(int x){
dfn[x]=low[x]=++tim;
siz[x]=1;
tot[x]=0;
int cnt=0;
for(int y:a[x]){
if(!dfn[y]){
f[y][0]=x;
//cout<<"there build a road: "<<x<<" "<<y<<endl;
dep[y]=dep[x]+1;
tarjan(y);
siz[x]+=siz[y];
low[x]=min(low[x],low[y]);
if(low[y]>=dfn[x]){
cnt++;
tot[x]+=siz[y];
}
}
else{
low[x]=min(low[x],dfn[y]);
}
}
if(f[x][0]==0&&cnt>=2)
v[x]=true;
if(f[x][0]!=0&&cnt>=1)
v[x]=true;
}
int lca(int x,int y){
if(dep[x]<dep[y])
swap(x,y);
for(int stp=30;stp>=0;stp--){
int xx=f[x][stp];
if(dep[xx]>=dep[y]){
x=xx;
}
}
if(x==y)
return x;
for(int stp=30;stp>=0;stp--){
int xx=f[x][stp],yy=f[y][stp];
if(xx==yy)
continue;
else
x=xx,y=yy;
}
x=f[x][0],y=f[y][0];
return x;
}
int getnum(int x,int y){
if(f[x][0]==0){
for(int yy:a[x]){
if(yy==f[x][0])
continue;
if(lca(y,yy)==x)
continue;
return n-siz[yy]-1;
}
}
int fa=lca(x,y);
if(fa==y){
return tot[x];
}
else if(fa==x){
for(int yy:a[x]){
if(yy==f[x][0])
continue;
if(lca(y,yy)==x)
continue;
if(low[yy]<dfn[x])
return 0;
else
return n-siz[yy]-1;
}
}
else{
return tot[x];
}
}
int main(){
cin>>T;
while(T--){
memset(v,0,sizeof(v));
memset(f,0,sizeof(f));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(siz,0,sizeof(siz));
tim=0;
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y;cin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int i=1;i<=n;i++){
if(!dfn[i]){
dep[i]=1;
tarjan(i);
}
}
for(int stp=1;stp<=30;stp++){
for(int i=1;i<=n;i++){
f[i][stp]=f[f[i][stp-1]][stp-1];
}
}
cin>>q;
for(int i=1;i<=q;i++){
int x,y;cin>>x>>y;
int ans=n;
if(v[x])
ans-=getnum(x,y);
if(v[y])
ans-=getnum(y,x);
cout<<ans<<endl;
}
}
return 0;
}
/*
1
5 5
1 2
1 3
2 4
4 5
2 5
1
2 5
*/
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 88360kb
input:
1 5 5 1 2 1 3 2 4 4 5 2 5 5 1 2 1 4 2 3 2 5 3 5
output:
2 4 3 3 5
result:
ok 5 number(s): "2 4 3 3 5"
Test #2:
score: -100
Wrong Answer
time: 426ms
memory: 86172kb
input:
100 10 25 7 4 1 10 7 2 3 4 5 7 9 10 10 5 8 10 6 7 9 1 4 2 2 6 8 5 6 9 5 9 7 1 2 1 4 1 9 8 8 3 1 8 4 8 2 10 3 1 3 6 100 6 4 10 8 5 4 7 8 3 10 5 9 6 9 6 8 2 10 10 9 6 9 1 8 3 6 10 9 1 4 10 8 1 6 5 1 5 10 9 1 3 5 8 7 3 2 3 9 2 9 9 4 2 10 8 4 6 2 2 1 7 4 3 6 6 5 10 6 1 4 5 1 7 10 7 1 8 5 3 8 7 5 3 9 5 4...
output:
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 ...
result:
wrong answer 2001st numbers differ - expected: '9', found: '10'