This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<vector>
#define gu getchar_unlocked
#define pf printf
using namespace std;
int sw()
{
int ret=0,wcz=gu();
while(wcz<'0'||wcz>'9')
wcz=gu();
while(wcz>='0'&&wcz<='9')
{
ret=ret*10+wcz-'0';
wcz=gu();
}
return ret;
}
int n,m;
vector<int>*pol;
bool *juzodw,*terodw;
int *stp_wch,*odp,it;
bool dfs(int t)
{
juzodw[t]=1;
terodw[t]=1;
for(int i:pol[t])
{
if(terodw[i]==1)
return 1;
if(juzodw[i]==0)
if(dfs(i))
return 1;
}
terodw[t]=0;
odp[++it]=t;
return 0;
}
int main()
{
n=sw();m=sw();
pol=new vector<int>[n+1];
juzodw=new bool[n+1];
terodw=new bool[n+1];
odp=new int[n+1];
stp_wch=new int[n+1];
while(m)
{
--m;
int a,b;
a=sw();b=sw();
pol[b].push_back(a);
stp_wch[a]++;
}
for(int i=1;i<=n;++i)
if(stp_wch[i]==0)
if(dfs(i))
{
pf("-1");
return 0;
}
if(it!=n)
{
pf("-1");
return 0;
}
pf("%d\n",n);
for(int i=1;i<=n;++i)
pf("%d 0\n",odp[i]);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |