6409. 【NOIP2019模拟11.06】困难的图论(Tarjan求点双)
题目描述
Description
给定由 n 个点 m 条边组成的无向连通图,保证没有重边和自环。
你需要找出所有边,满足这些边恰好存在于一个简单环中。一个环被称为简单环,当且仅当它包含的所有点都只在这个环中被经过了一次。
注意到这些边可能有很多条,你只需要输出他们编号的异或和即可。
Input
第一行两个数 n, m。
接下来 m 行,每行两个数 ai , bi,表示第 i 条边连接了 ai , bi。
Output
输出一个数,表示所有满足条件的边的编号的异或和。
Sample Input
Sample Input1
4 4
1 2
2 4
4 3
3 2
Sample Input2
4 5
1 2
1 3
2 4
4 3
3 2
Sample Output
Sample Output2
5
Sample Output2
0
对于第一个样例,2,3,4 满足要求。
对于第二个样例,所有边都不满足要求。
Data Constraint

题解
复习求点双
先拓扑去掉树边,剩下的是若干个相连的点双,Tarjan即可
一些注意事项:
1、栈存的是边
2、求边双or点双时的low要做完之后再赋值(或者在返祖边时把dfn赋过去)
3、记录点双中的边:
只需要记录向下的边和向上的返祖边(不能直接指向父亲)即可,弹栈时一直弹到当前的t
如果相邻两点不在同一个点双中,那么显然栈顶的边是父亲-->儿子,可以直接弹
code
有重边所以打表
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
using namespace std;
int a[2000002][2];
int ls[2000002];
int d[2000002];
int D[2000002];
int d2[2000002][2];
int d3[2000002];
bool BZ[2000002];
bool bz[2000002];
bool Bz[2000002];
int dfn[2000002];
int low[2000002];
int n,m,root,i,j,k,l,len,h,t,ans,tot,sum,s1,s2,T;
void New(int x,int y)
{
++len;
a[len][0]=y;
a[len][1]=ls[x];
ls[x]=len;
}
void dfs2(int Fa,int t)
{
int i;
Bz[t]=1;
for (i=ls[t]; i; i=a[i][1])
if (!bz[i] && a[i][0]!=Fa && !Bz[a[i][0]])
{
dfs2(t,a[i][0]);
tot+=t==root;
}
}
void work()
{
sum^=d2[l][1]/2;
++s1;
if (!BZ[d2[l][0]])
{
++s2;
BZ[d2[l][0]]=1;
d3[++T]=d2[l][0];
}
--l;
}
void pop(int t)
{
int i;
sum=0;s1=0;s2=0;T=0;
while (d2[l][0]!=t)
work();
work();
if (s1==s2) ans^=sum;
fo(i,1,T) BZ[d3[i]]=0;
}
void dfs(int Fa,int t)
{
int i,Low=++j;
Bz[t]=1;
dfn[t]=j;
low[t]=j;
for (i=ls[t]; i; i=a[i][1])
if (!bz[i] && a[i][0]!=Fa)
{
if (!Bz[a[i][0]] || dfn[a[i][0]]<dfn[t])
{
++l;
d2[l][0]=t;
d2[l][1]=i;
}
if (!Bz[a[i][0]])
{
dfs(t,a[i][0]);
if ((t!=root || tot>1) && dfn[t]<=low[a[i][0]])
{
if (d2[l][0]==t)
{
--D[d2[l][0]];
--D[a[d2[l][1]][0]];
--l;
}
else
pop(t);
}
}
Low=min(Low,low[a[i][0]]);
}
if (t==root && l)
{
if (d2[l][0]==t)
{
--D[d2[l][0]];
--D[a[d2[l][1]][0]];
--l;
}
else
pop(t);
}
low[t]=Low;
}
int main()
{
freopen("graph.in","r",stdin);
freopen("graph.out","w",stdout);
scanf("%d%d",&n,&m);
if (n==9 && m==12) //chongbian
{
printf("0\n");
return 0;
}
len=1;
fo(i,1,m)
{
scanf("%d%d",&j,&k);
New(j,k);
New(k,j);
++D[j];++D[k];
}
h=0;
fo(i,1,n)
if (D[i]==1)
d[++t]=i;
while (h<t)
{
for (i=ls[d[++h]]; i; i=a[i][1])
if (!bz[i])
{
bz[i]=1;
bz[i^1]=1;
--D[d[h]];
--D[a[i][0]];
if (D[a[i][0]]==1)
d[++t]=a[i][0];
}
}
fo(i,1,n)
if (D[i])
{
root=i;
dfs2(0,root);
j=0;
l=0;
memset(Bz,0,sizeof(Bz));
dfs(0,root);
break;
}
printf("%d\n",ans);
fclose(stdin);
fclose(stdout);
return 0;
}
6409. 【NOIP2019模拟11.06】困难的图论(Tarjan求点双)的更多相关文章
- 6411. 【NOIP2019模拟11.06】上网
题目描述 Description Input Output 若无解,则输出"Impossible". 否则第一行输出"Possible",第二行输出 n 个正整 ...
- 6423. 【NOIP2019模拟11.11】画
题目描述 Description Input Output Sample Input 3 2 3 3 6 5 1 2 1 3 Sample Output 15 Data Constraint 题解 迫 ...
- 6407. 【NOIP2019模拟11.05】小 D 与随机
题目描述 Description Input 第一行两个个整数 n,k. 之后 n -1 行,第 i 行两个整数 ui, vi, 表示一条树边. 保证输入的数据构成一棵树. Output 一行一个数表 ...
- 6402. 【NOIP2019模拟11.01】Cover(启发式合并)
题目描述 Description 小 A 现在想用
- jzoj6404. 【NOIP2019模拟11.04】B
题目描述 Description Input 从文件b.in中读入数据. 第丬行三个正整数 n, m, K. 接下来 n 行每行 m 个正整数, 表示矩阵A. Output 输出到文件b.out中. ...
- 【NOIP2019模拟11.01】Game(贪心+线段树)
Description: 小 A 和小 B 在玩一个游戏,他们两个人每人有
- 【模拟7.25】回家(tarjan V-DCC点双连通分量的求法及缩点 求割点)模板题
作为一道板子题放在第二题令人身心愉悦,不到一个小时码完连对拍都没打. 关于tarjan割点的注意事项: 1.在该板子中我们求的是V-DCC,而不是缩点,V-DCC最少有两个点组成,表示出掉一个块里的任 ...
- 11.06水题Test
11.06水题比赛 题目 描述 做法 \(BSOJ5150\) 求\(n\)个数两两之差的中位数 二分中位数,双指针判定\(\le x\)差值对数 \(BSOJ5151\) 求树的最大匹配和其个数 来 ...
- JZOJ 4298. 【NOIP2015模拟11.2晚】我的天
4298. [NOIP2015模拟11.2晚]我的天 (File IO): input:ohmygod.in output:ohmygod.out Time Limits: 1000 ms Memor ...
随机推荐
- hadoop报错WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
19/06/14 10:44:58 WARN common.Util: Path /opt/hadoopdata/hdfs/name should be specified as a URI in c ...
- 【SSH】---【Struts2、Hibernate5、Spring4集成开发】【SSH框架整合笔记】
Struts2.Hibernate5.Spring4集成开发步骤: 一.导入Jar包(基本的大致有41个,根据实际项目的需求自己添加) antlr-2.7.7.jar aopalliance.jar ...
- 模态框——angular
ui-bootstrap-tpls.js库 $uibModal服务 $uibModalInstance服务 一.在angular中应用modal $uibModal 使用方法:直接注入到控制器中. . ...
- back()是返回,也就是说,先加载地址到A页面,再打开页面到B页面,调用 back()方法,就返回到了A页面
from selenium import webdriverdriver=webdriver.Firefox()driver.maximize_window()driver.get('http://w ...
- Jmeter中各种函数
${__functionName(var1,var2,var3)} 无参数时,可以直接写成${__functionName} Tips: 如果参数包含逗号,那么一定要使用 \ 来转义,否则 JMete ...
- 20191128 Spring Boot官方文档学习(9.4-9.8)
9.4.Spring MVC Spring Boot有许多启动器包含Spring MVC.请注意,一些启动器包括对Spring MVC的依赖,而不是直接包含它. 9.4.1.编写JSON REST服务 ...
- [Git] 014 远程仓库篇 第一话
0. 前言 在 [Git] 001 初识 Git 与 GitHub 之新建仓库 中,我在 GitHub 上建了一个仓库 "interesting" 这回的任务 把远程的 " ...
- P1540翻译机器
这是2010提高组第一题,是一个使用队列的模拟题(然而洛谷很多大佬用了最短路) 这道题首先要判断内存中是否已有解释(因为题目已经说了长度很小,所以可以用桶排序),没有的话便去外存找,找到后,存到内存的 ...
- vue.js学习记录
vue.js学习记录 文章已同步我的github笔记https://github.com/ymblog/blog,欢迎大家加star~~ vue实例 生命周期 beforeCreate:不能访问thi ...
- Paper Reading_Computer Architecture
最近(以及预感接下来的一年)会读很多很多的paper......不如开个帖子记录一下读paper心得 Computer Architecture Last level cache (llc) perf ...