QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#168661 | #6543. Visiting Friend | PhantomThreshold# | WA | 1ms | 3436kb | C++20 | 2.3kb | 2023-09-08 18:36:32 | 2023-09-08 18:36:33 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxd=18;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n,m;
cin>>n>>m;
vector<vector<int>> G(n+5),tG(n+n+5);
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
int ind;
vector<int> dfn(n+5),low(n+5),ins(n+5);
stack<int> s;
int bcnt=0;
function<void(int,int)> dfs1=[&](int u,int p)
{
dfn[u]=low[u]=++ind;
s.push(u);ins[u]=1;
for(auto v:G[u])
{
if(v==p)continue;
if(not ins[v])
{
dfs1(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u])
{
++bcnt;
int vv;
do
{
vv=s.top();
// cerr<<"addedge "<<vv<<' '<<bcnt+n<<endl;
tG[bcnt+n].push_back(vv);
tG[vv].push_back(bcnt+n);
ins[vv]=2;
s.pop();
}
while(v!=vv);
// cerr<<"addedge "<<u<<' '<<bcnt+n<<endl;
tG[bcnt+n].push_back(u);
tG[u].push_back(bcnt+n);
}
}
else if(ins[v]==1)
{
low[u]=min(low[u],dfn[v]);
}
}
if(not p)s.pop();
};
dfs1(1,0);
vector<int> w(n+bcnt+5);
for(int i=1;i<=n;i++)w[i]=-1;
for(int i=n+1;i<=n+bcnt;i++)w[i]=tG[i].size();
vector<int> len(n+bcnt+5),dep(n+bcnt+5),pa(n+bcnt+5);
function<void(int)> dfs2=[&](int u)
{
for(auto v:tG[u])
{
if(not dep[v])
{
dep[v]=dep[u]+1;
len[v]=len[u]+w[v];
pa[v]=u;
dfs2(v);
}
}
};
len[1]=w[1];
dep[1]=1;
dfs2(1);
vector<vector<int>> jmp(maxd+5,vector<int>(n+bcnt+5));
for(int i=1;i<=n+bcnt;i++)
jmp[0][i]=pa[i];
for(int d=1;d<=maxd;d++)
for(int i=1;i<=n+bcnt;i++)
jmp[d][i]=jmp[d-1][jmp[d-1][i]];
auto getlca=[&](int u,int v)
{
if(dep[v]<dep[u])swap(u,v);
for(int d=maxd;d>=0;d--)
if(dep[jmp[d][v]]>=dep[u])
v=jmp[d][v];
if(u==v)return u;
for(int d=maxd;d>=0;d--)
if(jmp[d][u]!=jmp[d][v])
u=jmp[d][u],v=jmp[d][v];
return pa[u];
};
// for(int i=1;i<=n+bcnt;i++)cerr<<i<<' '<<w[i]<<' '<<len[i]<<endl;
int q;
cin>>q;
while(q--)
{
int u,v;
cin>>u>>v;
int lca=getlca(u,v);
// cerr<<"lca "<<lca<<endl;
cout<<2+len[u]+len[v]-len[lca]-len[pa[lca]]<<"\n";
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3436kb
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: 1ms
memory: 3420kb
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 2002nd numbers differ - expected: '10', found: '9'