本题有个小点需要注意,如果说它是多个相互不连通的图,也有可能形成一个可行解,多个环嘛。
然后剩下的,就是dfs去跑,如果跑出了返祖边,那么这个返祖边抵达的点,将改变原来的方向,剩下的就都是正方向,dfs直接跑就是了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7, maxM = 4e5 + 7;
int N, M, head[maxN], cnt;
struct Eddge
{
int nex, to;
Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxM];
inline void addEddge(int u, int v)
{
edge[cnt] = Eddge(head[u], v);
head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
bool vis[maxN] = {false};
int fa[maxN] = {0}, ans[maxN] = {0};
bool bak;
void dfs(int u, int father)
{
fa[u] = father; vis[u] = true;
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(v == father) continue;
if(!vis[v])
{
ans[v] = u;
dfs(v, u);
}
else if(!bak)
{
ans[v] = u;
bak = true;
int now = v, las = fa[v];
while(las)
{
ans[las] = now;
now = las;
las = fa[now];
}
}
}
}
inline void init()
{
cnt = 0;
for(int i=1; i<=N; i++) head[i] = -1;
}
int main()
{
scanf("%d%d", &N, &M);
init();
for(int i=1, u, v; i<=M; i++)
{
scanf("%d%d", &u, &v);
_add(u, v);
}
for(int i=1; i<=N; i++)
{
if(!vis[i])
{
bak = false;
dfs(i, 0);
if(!bak) { printf("NIE\n"); return 0; }
}
}
printf("TAK\n");
for(int i=1; i<=N; i++) printf("%d\n", ans[i]);
return 0;
}