QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#820420 | #9875. Don't Detect Cycle | kdyl | RE | 1ms | 3936kb | C++14 | 3.5kb | 2024-12-18 21:23:18 | 2024-12-18 21:23:18 |
Judging History
answer
#include<bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <math.h>
#include <cstdio>
#define int long long
using namespace std;
const int inf=1e18;
bool M1;
int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=4e3+5;
struct edge{
int to,nxt,id;
}e[N<<1];
struct node{
int u,v,id;
}a[N];
int n,m,cnt,ttt,head[N],dfn[N],low[N],flag[N],ok[N],vis[N],du[N];
queue<vector<int> >q;
vector<int>ans,now,g,ANS;
void tarjan(int u){
dfn[u]=low[u]=++ttt;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to,id=e[i].id;
if(!dfn[v]){
flag[id]=1;
tarjan(v),low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]){
if(!ok[id])ans.push_back(id);
ok[id]=1;
}
}
else if(!flag[id])low[u]=min(low[u],dfn[v]);
}
}
vector<int>E;
void dfs(int u){
vis[u]=1;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to,id=e[i].id;
if(ok[id])continue;
ok[id]=1;
E.push_back(e[i].id);
dfs(v);
}
}
void work(){
ttt=0;
for(auto i:now){
if(!dfn[i])tarjan(i);
}
for(auto i:now){
if(!vis[i]){E.clear();
dfs(i),q.push(E);
}
}
}
void add(int u,int v,int id){
e[++cnt].to=v;e[cnt].nxt=head[u];e[cnt].id=id;head[u]=cnt;
}
int _ans[N];
void solve(){
n=read();m=read();cnt=0;for(int i=1;i<=n;i++)head[i]=0;
ans.clear();now.clear();ANS.clear();
for(int i=1;i<=n;i++)now.push_back(i),dfn[i]=low[i]=0,du[i]=0,vis[i]=0;
for(int i=1;i<=m;i++)flag[i]=0,ok[i]=0;
for(int i=1;i<=m;i++){
int u,v;u=read();v=read();add(u,v,i);add(v,u,i);a[i].u=u;a[i].v=v;a[i].id=i;
}
while(!q.empty())q.pop();
work();
while(!q.empty()){
g=q.front();q.pop();cnt=0;now.clear();
if(!g.size())continue;
int ID=-1;
for(int i=0;i<g.size();i++){
int id=g[i];flag[id]=0;ok[id]=0;
int u=a[id].u,v=a[id].v;now.push_back(u);now.push_back(v);
head[u]=0;head[v]=0;du[u]++;du[v]++;
}
for(int i=0;i<g.size();i++){
int idt=g[i];
int u=a[idt].u,v=a[idt].v,id=a[idt].id;vis[u]=0;vis[v]=0;
if(du[u]==2&&du[v]==2){
ANS.push_back(id);ID=id;break;
}
}
if(ID==-1){
puts("-1");return;
}
cnt=0;
for(int i=0;i<g.size();i++){
int idt=g[i];
int u=a[idt].u,v=a[idt].v,id=a[idt].id;du[u]=0;du[v]=0;
if(ID==id)continue;
dfn[u]=low[u]=0;dfn[v]=low[v]=0;
add(u,v,id);add(v,u,id);
}
work();
}
reverse(ANS.begin(),ANS.end());
int C=0;
for(int i=0;i<ans.size();i++)cout<<ans[i]<<" ",_ans[++C]=ans[i];
for(int i=0;i<ANS.size();i++)cout<<ANS[i]<<" ",_ans[++C]=ANS[i];
sort(_ans+1,_ans+1+C);
for(int i=1;i<=C;i++)assert(_ans[i]==i);
putchar('\n');
}
bool M2;
signed main(){
//freopen("data.in","r",stdin);
//ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int T;T=read();while(T--)solve();
cerr<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC<<"s"<<endl;
cerr<<"Memory:"<<(&M1-&M2)/1024/1024<<"MB"<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3924kb
input:
1 4 4 1 2 2 3 3 4 4 2
output:
1 2 3 4
result:
ok Correct
Test #2:
score: 0
Accepted
time: 1ms
memory: 3936kb
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 1 3 2 6 4 10 9 8 7 5 -1
result:
ok Correct
Test #3:
score: -100
Runtime Error
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:
2723 494 1770 2783 1573 2251 2792 1051 2701 2571 876 2536 1759 2468 2856 2532 1699 1815 1519 2410 2220 1939 451 1640 674 1523 2394 1159 2093 2371 1937 528 2403 216 2487 1224 2118 2128 1507 2880 2859 2886 1223 2740 1467 1623 1440 594 1400 2462 2537 127 2284 1141 2782 2167 325 1490 2743 2508 2873 1694...