题目:http://codeforces.com/contest/949/problem/C

把一个点指向修改它会影响到的点就可以做了;

有取模,所以多出一些要注意的地方,首先是可能出现环,所以需要 tarjan 求边双;

其次,边集数组的大小应该开成两倍,因为取模可能导致一对 ci 互相连边;

然后找出不影响别的点的、最小的边双,输出即可;

而我竟然把 tarjan 都少写了一个 top-- !真是对自己无语了...

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int const maxn=1e5+;
int n,m,h,a[maxn],hd[maxn],cr,ct,col[maxn],sta[maxn],top,dfn[maxn],low[maxn],tim,deg[maxn];
bool vis[maxn];
vector<int>scc[maxn];
struct N{
int to,nxt;
N(int t=,int n=):to(t),nxt(n) {}
}ed[maxn<<];
void add(int x,int y){ed[++ct]=N(y,hd[x]); hd[x]=ct;}
void tarjan(int x)
{
dfn[x]=low[x]=++tim; sta[++top]=x; vis[x]=;
for(register int i=hd[x];i;i=ed[i].nxt)
{
int u=ed[i].to;
if(!dfn[u])
{
tarjan(u); low[x]=min(low[x],low[u]);
}
else if(vis[x])low[x]=min(low[x],dfn[u]);
}
if(low[x]==dfn[x])
{
cr++;
while(sta[top]!=x)
{
int y=sta[top]; top--;
vis[y]=; col[y]=cr;
scc[cr].push_back(y);
}
top--;//!
vis[x]=; col[x]=cr; scc[cr].push_back(x);
}
}
int main()
{
scanf("%d%d%d",&n,&m,&h);
for(register int i=;i<=n;i++)scanf("%d",&a[i]);
for(register int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
if((a[x]+)%h==a[y])add(x,y);
if((a[y]+)%h==a[x])add(y,x);
}
for(register int i=;i<=n;i++)
if(!dfn[i])tarjan(i);
for(register int i=;i<=n;i++)
for(register int j=hd[i];j;j=ed[j].nxt)
{
int u=ed[j].to;
if(col[i]!=col[u])deg[col[i]]++;
}
int mn=0x3f3f3f3f,tag;
for(register int i=;i<=cr;i++)
if(!deg[i]&&scc[i].size()<mn)mn=scc[i].size(),tag=i;
printf("%d\n",scc[tag].size());
for(register int j=;j<scc[tag].size();j++)
printf("%d ",scc[tag][j]);
return ;
}

最新文章

  1. C#基础01
  2. MySQL 行子查询(转)
  3. GPRS连接失败问题
  4. vb的property 和event
  5. maven项目打包
  6. 再学习sqlhelper
  7. ecshop 去版权
  8. C#的常见算法(面试)
  9. Cesium中Clock控件及时间序列瓦片动态加载
  10. vue与avuex
  11. scapy官方文档
  12. SharePoint 多行文本字段设置默认值
  13. linux 设置默认网关永久
  14. adb install使用说明
  15. docker 创建基础镜像
  16. Codeforces Round #254 (Div. 1) A. DZY Loves Physics 智力题
  17. tomcat原理解析(二):整体架构
  18. LeetCode OJ:Reverse Nodes in k-Group(K个K个的分割节点)
  19. zoj2432
  20. HTML5移动Web开发实战 PDF扫描版​

热门文章

  1. vue组件---插槽
  2. Deepin系统关于每次启动终端都要输入source /etc/profile的问题
  3. 解决docker容器启动时候无法映射端口的问题
  4. 大数低速幂运算模板(c++)+python大数幂
  5. vue-cli中src/main.js 的作用
  6. Django-利用paginator模块实现分页
  7. POJ -棋盘问题
  8. 【Codeforces 1009D】Relatively Prime Graph
  9. 【Codeforces 1037D】Valid BFS?
  10. 学渣乱搞系列之Tarjan模板合集