正题

题目链接:https://www.luogu.com.cn/problem/P6378


题目大意

给出\(n\)个点\(m\)条边的一张无向图,图中有\(k\)种颜色的点。

要求每种颜色选择一个点作为关键点,满足每条边两边至少有一个关键点

求是否有满足的方案

\(1\leq n,m,k\leq 10^6\)


解题思路

如果想到\(2-SAT\)的话就挺好解决的了。

然后一个经典的问题是一堆点里面选了一个点就不能选其他点。

可以考虑优化建图,搞一些前缀点和一些后缀点就好了

时间复杂度\(O(n)\)


code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
const int N=5e6+10;
struct node{
int to,next;
}a[N<<1];
int n,m,k,cnt,tot,dfc,cfc;
int ls[N],dfn[N],low[N],col[N];
bool ins[N];vector<int> v[N];
stack<int> s;
void addl(int x,int y){
a[++tot].to=y;
a[tot].next=ls[x];
ls[x]=tot;return;
}
void tarjan(int x){
dfn[x]=low[x]=++dfc;
s.push(x);ins[x]=1;
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(!dfn[y]){
tarjan(y);
low[x]=min(low[x],low[y]);
}
else if(ins[y])
low[x]=min(low[x],dfn[y]);
}
if(low[x]==dfn[x]){
++cfc;
while(s.top()!=x){
col[s.top()]=cfc;
ins[s.top()]=0;
s.pop();
}
col[s.top()]=cfc;
ins[s.top()]=0;
s.pop();
}
return;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++){
int x,y;scanf("%d%d",&x,&y);
addl(x*2-1,y*2);addl(y*2-1,x*2);
}
cnt=2*n;
for(int i=1;i<=k;i++){
int w,x;scanf("%d",&w);
v[i].push_back(0);
for(int j=1;j<=w;j++){
scanf("%d",&x);
v[i].push_back(x);
}
cnt++;addl(cnt,v[i][1]*2-1);
for(int j=2;j<=w;j++){
addl(v[i][j]*2,cnt);
++cnt;addl(cnt,cnt-1);
addl(cnt,v[i][j]*2-1);
}
cnt++;addl(cnt,v[i][w]*2-1);
for(int j=w-1;j>=1;j--){
addl(v[i][j]*2,cnt);
++cnt;addl(cnt,cnt-1);
addl(cnt,v[i][j]*2-1);
}
}
for(int i=1;i<=cnt;i++)
if(!dfn[i])tarjan(i);
for(int i=1;i<=n;i++)
if(col[2*i]==col[2*i-1])return puts("NIE")&0;
puts("TAK");return 0;
}

最新文章

  1. BZOJ1017: [JSOI2008]魔兽地图DotR
  2. ios几个重要方法
  3. 使用myeclipse建立maven项目(重要)
  4. 转载:Clear Float
  5. jquery实现公告上下滚动显示
  6. 用ModelSim仿真SDRAM操作
  7. (step6.1.5)hdu 1233(还是畅通工程——最小生成树)
  8. ios 屏幕方向的设置
  9. SQL-with as基本用法(源码DEMO)
  10. HTML脚本配置Android自动化测试
  11. main函数的两个参数
  12. Redis详解(五)------ redis的五大数据类型实现原理
  13. Java 8 特性 —— 方法引用
  14. WEB前端开发记录PS常见操作
  15. 第十四届智能车培训 PLL锁相环
  16. ServletContextListener的作用
  17. WPF 4.5 is here : check out the new features !
  18. yum仓库中源的配置与使用
  19. Typescript学习总结之模块
  20. mysql字符生命周期

热门文章

  1. 如何在github上传本地项目代码
  2. Redis3.0.0集群一键脚本 -by古斌
  3. 虚拟机--第一章走进java--(抄书)
  4. 搭建zabbix监控系统详解
  5. (int)a、&amp;a、(int)&amp;a、(int&amp;)a的区别,很偏僻的题
  6. CentOS7部署SSH服务
  7. Golang gomail 发送邮件 --初使用
  8. 去除所有js,html,css代码
  9. 登录用户出现‘’-bash-4.2$‘’的问题解决
  10. idea无法使用中文输入法输入