比赛 |
EYOI与SBOI开学欢乐赛8th |
评测结果 |
WAWWWWWWWWWWWWWWWWWW |
题目名称 |
菜肴制作 |
最终得分 |
5 |
用户昵称 |
遥时_彼方 |
运行时间 |
0.876 s |
代码语言 |
C++ |
内存使用 |
4.58 MiB |
提交时间 |
2022-09-26 20:49:59 |
显示代码纯文本
#include<bits/stdc++.h>
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define drep(x,y,z) for(int x=y;x>=z;x--)
#define ull unsigned long long
#define ll long long
using namespace std;
inline int read()
{
int x=0;bool flag=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')flag=0;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
if(flag) return x;
return ~(x-1);
}
inline void write(int x)
{
if(x<0) {x=~(x-1);putchar('-');}
if(x>9) write(x/10);
putchar(x%10+'0');
}
//
const int N=1e5+50;
int tc,nc,mc;
int YN;
int ans[N],ansj;
struct SR
{
int s1;
int s2;
}s[N];
bool cmp(SR x,SR y)
{
if(x.s1!=y.s1) return x.s1<y.s1;
return x.s2<y.s2;
}
int ha[N],hb[N];
struct qxx
{
int ar;
int nx;
}a[N<<1],b[N<<1];//a:往下,b:往上
int aj;
inline void ADD(int x,int y)
{
a[++aj].ar=x;
a[aj].nx=ha[y];
ha[y]=aj;
b[aj].ar=y;
b[aj].nx=hb[x];
hb[x]=aj;
}
int sz[N];//前置条件的多少
priority_queue<int,vector<int>,greater<int> > q;//小根堆
int j[N];
void BL(int cl)
{
j[cl]=1;
for(int i=ha[cl];i;i=a[i].nx)
{
sz[cl]++;
if(j[a[i].ar]) YN=1;
if(!sz[a[i].ar]&&ha[a[i].ar]) BL(a[i].ar);
}
j[cl]=0;
}
void BCL(int cl)
{
j[cl]=1;
if(!sz[cl])
{
q.push(cl);
return;
}
for(int i=ha[cl];i;i=a[i].nx)
{
if(j[a[i].ar]) continue;
BCL(a[i].ar);
}
}
void bfs()
{
int z;
while(q.size())
{
z=q.top();
q.pop();
ans[++ansj]=z;
for(int i=hb[z];i;i=b[i].nx)
{
sz[b[i].ar]--;
if(j[b[i].ar]&&sz[b[i].ar]==0) q.push(b[i].ar);
}
}
}
void solve()
{
memset(ha,0,sizeof(ha));
memset(hb,0,sizeof(hb));
memset(sz,0,sizeof(sz));
memset(j,0,sizeof(j));
aj=ansj=YN=0;
nc=read(),mc=read();
rep(i,1,mc) s[i].s1=read(),s[i].s2=read();
sort(s+1,s+1+mc,cmp);
rep(i,2,mc) if(s[i-1].s1==s[i].s1&&s[i-1].s2==s[i].s2) s[i-1].s1=s[i-1].s2=0;
rep(i,1,mc) if(s[i].s1&&s[i].s2) ADD(s[i].s1,s[i].s2);
rep(i,1,nc) if(!sz[i]&&ha[i]) BL(i);
if(YN==1)
{
cout<<"Impossible!\n";
return;
}
rep(i,1,nc) if(!j[i]) BCL(i),bfs();
rep(i,1,nc) write(ans[i]),putchar(32);
putchar(10);
}
int main()
{
freopen("dishes.in","r",stdin);
freopen("dishes.out","w",stdout);
tc=read();
rep(i,1,tc) solve();
return 0;
}