QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#820210#9875. Don't Detect CyclekdylWA 0ms4092kbC++143.2kb2024-12-18 20:09:022024-12-18 20:09:05

Judging History

你现在查看的是最新测评结果

  • [2024-12-18 20:09:05]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:4092kb
  • [2024-12-18 20:09:02]
  • 提交

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;
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){
    for(int i=head[u];i;i=e[i].nxt){
        int v=e[i].to,id=e[i].id;
        if(vis[id]||ok[id])continue;
        vis[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;
}
void solve(){
    n=read();m=read();cnt=0;for(int i=1;i<=n;i++)head[i]=0;
    vector<int>G;ans.clear();now.clear();
    for(int i=1;i<=n;i++)now.push_back(i),dfn[i]=low[i]=0;
    for(int i=1;i<=m;i++)flag[i]=0,ok[i]=0,vis[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;
    }
    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;vis[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;
            if(du[u]==2&&du[v]==2){
                ans.push_back(id);ID=idt;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(idt==ID)continue;
            dfn[u]=low[u]=0;dfn[v]=low[v]=0;
            add(u,v,id);add(v,u,id);
        }
        work();
    }
    for(int i=0;i<ans.size();i++)cout<<ans[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: 0ms
memory: 3808kb

input:

1
4 4
1 2
2 3
3 4
4 2

output:

1 4 2 3 

result:

ok Correct

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 4092kb

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 5 6 4 10 7 9 8 
-1

result:

wrong answer Wrong - you detected cycles