题意:
给你 n n n个点 m m m调变的无向图被分成 k k k个部分,每个部分包含若干点,请选择一些关键点,使得每个部分恰好有一个关键点,且每条边至少有一个是关键点。
1 ≤ k , w ≤ n ≤ 1 e 6 1\le k,w\le n\le 1e6 1≤k,w≤n≤1e6
思路:
首先每条边至少一个是关键点,这个是 2 − s a t 2-sat 2−sat的套路建边了,假设这个边是 ( a , b ) (a,b) (a,b),那么应该 a ′ − > b , b ′ − > a a'->b,b'->a a′−>b,b′−>a。
其次每个部分恰好一个关键点,这个看起来并不是很好建图,因为暴力建图的话是 n 2 n^2 n2级别的边,显然不行。我们对于这种区间的建边,不是线段树优化就是前缀和优化,这里使用前缀和优化。
设 p r e i , j pre_{i,j} prei,j代表第 i i i个部分的前 j j j个点是否存在关键点,以下简称为 p r e j pre_j prej。
根据这个来限制一下每个部分,写出如下限制:
(
1
)
(1)
(1)
a
j
−
>
p
r
e
j
,
p
r
e
j
′
−
>
a
j
′
a_j->pre_j,pre_j'->a_j'
aj−>prej,prej′−>aj′。
( 2 ) (2) (2) p r e j − 1 − > a j ′ , a j − > p r e j − 1 ′ pre_{j-1}->a_j',a_j->pre_{j-1}' prej−1−>aj′,aj−>prej−1′。
( 3 ) (3) (3) p r e j − 1 − > p r e j , p r e j ′ − > p r e j − 1 ′ pre_{j-1}->pre_j,pre_j'->pre_{j-1}' prej−1−>prej,prej′−>prej−1′。
不难发现这些条件已经足够了,直接跑个 2 − s a t 2-sat 2−sat即可。
// Problem: F. Radio Stations
// Contest: Codeforces - Codeforces Round #585 (Div. 2)
// URL: https://codeforces.com/contest/1215/problem/F
// Memory Limit: 256 MB
// Time Limit: 7000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<assert.h>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=4001010,M=N*20,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,m,k;
int e[M],ne[M],h[N],idx;
int dfn[N],low[N],id[N],tot,cnt;
int stk[N],top;
bool in[N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void tarjan(int u)
{
dfn[u]=low[u]=++tot;
stk[++top]=u; in[u]=true;
for(int i=h[u];~i;i=ne[i])
{
int ver=e[i];
if(!dfn[ver])
{
tarjan(ver);
low[u]=min(low[u],low[ver]);
}
else if(in[ver]) low[u]=min(low[u],dfn[ver]);
}
if(dfn[u]==low[u])
{
int y; ++cnt;
do
{
y=stk[top--];
in[y]=false; id[y]=cnt;
}while(y!=u);
}
}
int yes(int x) { return x<<1; }
int no(int x) { return x<<1|1; }
void link(int x,int y) {
add(x,y);
add(y^1,x^1);
}
bool check()
{
for(int i=0;i<=n+n;i++) if(id[yes(i)]==id[no(i)]) return false;
return true;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
scanf("%d%d%d",&n,&m,&k);
idx=0;
memset(h,-1,sizeof(h));
for(int i=1;i<=m;i++) {
int a,b; scanf("%d%d",&a,&b);
a--; b--;
link(no(a),yes(b));
}
for(int i=1,st=n;i<=k;i++) {
int cnt; scanf("%d",&cnt);
for(int j=st;j<=st+cnt-1;j++) {
int x; scanf("%d",&x);
x--;
link(yes(x),yes(j));
if(j!=st) link(yes(j-1),no(x));
if(j!=st+cnt-1) link(yes(j),yes(j+1));
}
st+=cnt;
}
for(int i=0;i<=no(n+n);i++) if(!dfn[i]) tarjan(i);
if(check()) {
puts("TAK");
} else puts("NIE");
return 0;
}