QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#201100 | #6543. Visiting Friend | ucup-team1209# | WA | 15ms | 65176kb | C++20 | 2.3kb | 2023-10-05 11:12:58 | 2023-10-05 11:12:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,y,x) for (int i=(y);i>=(x);i--)
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
template<typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template<typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
void file() {
#ifdef zqj
freopen("a.in","r",stdin);
#endif
}
typedef long long ll;
#define sz 1101010
int n,m;
vector<int>V[sz];
vector<int>V2[sz];
int dfn[sz],low[sz],cc,T;
stack<int>st;
void tarjan(int x,int fa) {
dfn[x]=low[x]=++cc;
st.push(x);
for (auto v:V[x]) if (v!=fa) {
if (!dfn[v]) tarjan(v,x),chkmin(low[x],low[v]);
else chkmin(low[x],dfn[v]);
}
if (fa&&low[x]>=dfn[fa]) {
int y; ++T;
do {
y=st.top(); st.pop();
V2[T+n].push_back(y),V2[y].push_back(T+n);
} while (y!=x);
V2[T+n].push_back(fa),V2[fa].push_back(T+n);
}
}
int siz[sz],son[sz],top[sz],fa[sz],pre[sz];
void dfs1(int x,int fa) {
if (x<=n) siz[x]=1;
::fa[x]=fa;
for (auto v:V2[x]) if (v!=fa) {
dfs1(v,x),siz[x]+=siz[v];
if (siz[v]>siz[son[x]]) son[x]=v;
}
}
void dfs2(int x,int fa,int tp) {
dfn[x]=++cc; pre[cc]=x;
if (son[x]) dfs2(son[x],x,tp);
for (auto v:V2[x]) if (v!=fa&&v!=son[x]) {
dfs2(v,x,v);
}
low[x]=cc;
}
void work() {
cin>>n>>m;
int x,y;
rep(i,1,m) cin>>x>>y,V[x].push_back(y),V[y].push_back(x);
T=cc=0;
tarjan(1,0);
cc=0;
dfs1(1,0),dfs2(1,0,1);
int Q; cin>>Q;
while (Q--) {
cin>>x>>y;
if (dfn[x]>dfn[y]) swap(x,y);
if (low[x]>=low[y]) {
int z=y,zz=-1;
while (top[z]!=top[x]) zz=top[z],z=fa[top[z]];
int ans;
if (z!=x) zz=pre[dfn[x]+1];
ans=siz[zz]-siz[y]+2;
cout<<ans<<'\n';
}
else cout<<n-siz[x]-siz[y]+2<<'\n';
}
rep(i,1,n+T) V[i].clear(),V2[i].clear(),dfn[i]=low[i]=son[i]=siz[i]=top[i]=pre[i]=fa[i]=0;
cc=T=0;
while (st.size()) st.pop();
}
int main() {
file();
ios::sync_with_stdio(false),cin.tie(0);
int T; cin>>T;
while (T--) work();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 15ms
memory: 65176kb
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: 0
Accepted
time: 6ms
memory: 65020kb
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:
ok 10000 numbers
Test #3:
score: -100
Wrong Answer
time: 4ms
memory: 64628kb
input:
100 10 10 5 10 6 4 2 9 9 8 9 10 1 5 7 2 7 4 9 3 6 5 100 4 7 4 5 3 10 7 10 4 10 6 7 3 2 5 2 1 2 10 8 3 1 10 5 6 1 2 4 5 9 6 7 1 8 5 6 7 9 9 8 9 3 6 1 9 10 4 10 1 4 2 7 8 9 9 4 7 9 6 7 9 1 9 6 9 1 4 3 9 6 2 3 9 5 6 7 7 8 8 1 4 8 9 6 8 2 4 5 3 5 4 8 5 10 6 4 8 6 8 7 7 2 3 4 4 8 1 2 3 2 2 10 4 10 3 6 8 ...
output:
10 9 10 10 10 10 10 9 10 10 10 9 10 10 7 10 10 9 8 2 2 10 8 10 10 10 2 8 8 10 8 8 8 10 8 10 7 10 10 10 10 8 10 9 9 10 9 10 10 10 10 10 10 10 10 10 10 10 9 2 8 10 10 10 10 10 10 2 10 10 10 9 8 9 10 8 8 10 10 10 10 10 10 10 10 2 9 9 9 8 9 8 10 10 2 9 10 10 9 10 9 6 7 9 9 9 10 9 5 7 5 10 9 9 9 10 10 6 ...
result:
wrong answer 142nd numbers differ - expected: '2', found: '3'