QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#814188 | #9875. Don't Detect Cycle | ucup-team3161# | WA | 133ms | 12572kb | C++17 | 2.6kb | 2024-12-14 15:50:12 | 2024-12-14 15:50:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define pii pair<int,int>
#define eb emplace_back
const int N=1e5+5;
mt19937_64 rand1;
int T,n,m,_u[N],_v[N],dg[N],ans[N];
bool vse[N];
int fa[N],d[N];
int w1[N];ull w[N],we[N];
vector<ull> z[N];
bool vs[N],tr[N];
vector<pii> e[N];
void dfs(int u,int f)
{
fa[u]=f;
d[u]=d[f]+1;
vs[u]=1;
for(auto [v,id]:e[u])
if(!vse[id] && !vs[v])
tr[id]=1,dfs(v,u);
}
void dfs1(int u,int f)
{
vs[u]=1;
for(auto [v,id]:e[u])
if(!vse[id] && !vs[v])
{
dfs1(v,u);
w[u]^=w[v];
we[u]^=we[v];
w1[u]+=w1[v];
}
}
bool chk(int u,ull w)
{
auto it=lower_bound(z[u].begin(),z[u].end(),w);
if(it==z[u].end() || it+1==z[u].end()) return 0;
return *(it+1)==w;
}
int get()
{
int r=0;
for(int i=1;i<=n;++i) if(dg[i]) {r=i;break;}
fill(tr+1,tr+m+1,0);
fill(vs+1,vs+n+1,0);
dfs(r,0);
fill(w+1,w+n+1,0);
fill(we+1,we+n+1,0);
fill(w1+1,w1+n+1,0);
for(int i=1;i<=m;++i)
if(!vse[i] && !tr[i])
{
int u=_u[i],v=_v[i];
if(d[u]<d[v]) swap(u,v);
ull t=rand1();
w[u]^=t;
w[fa[v]]^=t;
we[u]^=t;
we[v]^=t;
++w1[u];
--w1[fa[v]];
}
fill(vs+1,vs+n+1,0);
dfs1(r,0);
for(int i=1;i<=n;++i) z[i].clear();
for(int i=1;i<=m;++i)
if(!vse[i] && tr[i])
{
int u=_u[i],v=_v[i];
if(d[u]<d[v]) swap(u,v);
z[u].eb(we[u]);
z[v].eb(we[u]);
}
for(int i=1;i<=n;++i)
sort(z[i].begin(),z[i].end());
for(int i=1;i<=m;++i) if(!vse[i])
{
int u=_u[i],v=_v[i];
if(d[u]<d[v]) swap(u,v);
if(w[u]!=w[v]) continue;
if(w1[u]<2 && w1[v]<2) return i;
if(tr[i] && chk(u,we[u]) && chk(v,we[u])) return i;
}
return 0;
}
void slv()
{
scanf("%d %d",&n,&m);
fill(dg+1,dg+m+1,0);
for(int i=1;i<=n;++i) e[i].clear();
for(int i=1,u,v;i<=m;++i)
{
scanf("%d %d",&u,&v);
_u[i]=u;_v[i]=v;
++dg[u];++dg[v];
e[u].eb(v,i);
e[v].eb(u,i);
}
fill(vse+1,vse+m+1,0);
for(int i=m,t;i;--i)
{
t=ans[i]=get();
if(!t) {puts("-1");return;}
vse[t]=1;
--dg[_u[t]];--dg[_v[t]];
}
for(int i=1;i<=m;++i) printf("%d ",ans[i]);
puts("");
}
int main()
{
scanf("%d",&T);
while(T--) slv();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 12572kb
input:
1 4 4 1 2 2 3 3 4 4 2
output:
4 3 1 2
result:
ok Correct
Test #2:
score: 0
Accepted
time: 2ms
memory: 11188kb
input:
4 4 5 1 2 2 3 3 4 3 1 1 4 5 3 1 2 2 3 3 4 9 10 3 5 1 8 5 8 4 9 6 7 7 9 1 2 1 4 2 4 4 6 8 10 1 4 3 8 2 5 3 4 1 5 5 8 2 8 5 7 4 5 3 7
output:
-1 3 2 1 10 9 8 4 2 7 6 5 3 1 -1
result:
ok Correct
Test #3:
score: -100
Wrong Answer
time: 133ms
memory: 11208kb
input:
50 3214 2907 970 1929 2860 3033 1322 2296 931 1192 861 2505 831 2469 231 2549 1 2306 1765 1842 999 3171 177 2007 1798 1894 827 3180 673 1738 1163 1573 2213 2781 2766 3200 1663 2197 1797 2281 315 2637 442 2689 558 2874 1520 2591 651 1923 1133 2920 1747 2412 1104 1528 313 2487 632 3124 660 2182 1581 2...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 12 11 10 8 17 15 5 3 16 13 14 7 6 1 2 9 4 -1 13 10 14 8 4 6 18 16 15 9 7 17 12 11 5 3 2 1 -1 -1 18 16 15 19 20 12 17 11 10 9 14 8 1 2 13 6 3 5 7 4 -1 -1 -1 -1 -1 -1 -1 -1 4 16 11 7 3 18 10 17 14 15 8 6 13 2 5 12 9 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 19 11 9 20 18 16 2 1...
result:
wrong answer Wrong - you can add all edges, but found: -1