http://acm.sgu.ru/problem.php?contest=0&problem=219

题目大意:

如果指向某个点的边权全都为正数,那么这个点就是可点燃的,点燃操作把入弧权值-1,出弧权值都+1,

如果在某种点燃序列之后还可以再点燃一些点使得这个点还可以点燃,那么这个点在这种点燃序列之后存活

如果在任何点燃序列之后都还可以再点燃一些点使得这个点还可以点燃,那么这个点可存活

现在求所有点是否可存活

思路:

考虑不可存活的点:对于某个状态,对于不可存活的点,要想使得没有序列可以使它被点燃,那么有边指向它的点里一定有不可存活的点,且这条边权值为0,

如果有一个边权值都为0的环,那么在这条环上,由于权值都为0,所以这个环上的都是不可存活的点.

所以:先通过求边权值都为0的环确定一些初始点,由这些不可存活点出发到达的点,都在某个序列下因为这些不可存活点不能提供权值而不能存活

注意:

1. 有自环

2. tarjian找环需要注意更新dfn值的点在stack内,否则对于我写的形式

会有这种情况不正确

#include  <cstdio>
#include <stack>
#include <cstring>
using namespace std;
const int maxn=1e3+3;
const int maxm=5e4+4;
int n,m; int first[maxn],head[maxn];
struct edge{
int t,nxt;
}e[maxm],g[maxm]; int low[maxn],dp[maxn],depth;
int alive[maxn];
bool in[maxn]; void addedge(int f,int t,int c,int ind){
if(c==0){
g[ind].nxt=head[f];
g[ind].t=t;
head[f]=ind;
}
e[ind].nxt=first[f];
e[ind].t=t;
first[f]=ind;
} stack<int> st;
void tarjian(int s){
low[s]=dp[s]=++depth;
in[s]=true;st.push(s);
for(int p=head[s];p!=-1;p=g[p].nxt){
int t=g[p].t;
if(t==s){
alive[s]=0;
}
if(dp[t]==0){
tarjian(t);
low[s]=min(low[s],low[t]);
}
else if(in[t]){//ATTHENTION:
low[s]=min(low[s],dp[t]);
}
} bool single=true;
if(low[s]==dp[s]){
while(st.top()!=s){
single=false;
alive[st.top()]=0; in[st.top()]=false;st.pop();
}
if(!single){
alive[st.top()]=0;
}
in[st.top()]=false;st.pop();
}
} void dfs(int s){
for(int p=first[s];p!=-1;p=e[p].nxt){
int t=e[p].t;
if(alive[t]==1){
alive[t]=0;
dfs(t);
}
} } int main(){
scanf("%d%d",&n,&m);
memset(first,-1,sizeof(first));
memset(head,-1,sizeof(head));
fill(alive,alive+n+1,1);
for(int i=0;i<m;i++){
int f,t,c;
scanf("%d%d%d",&f,&t,&c);
addedge(f,t,c,i);
} for(int i=1;i<=n;i++){
if(dp[i]==0){
tarjian(i);
}
} for(int i=1;i<=n;i++){
if(alive[i]==0){
dfs(i);
}
} for(int i=1;i<=n;i++){
printf("%d\n",alive[i]);
} return 0;
}

  

SGU 219 Synchrograph tarjian找环,理解题意,图论 难度:3的更多相关文章

  1. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  2. Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂

    题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...

  3. # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)

    「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...

  4. [图中找环] Codeforces 659E New Reform

    New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  5. [hdu5348]图上找环,删环

    http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...

  6. zstu.4191: 无向图找环(dfs树 + 邻接表)

    4191: 无向图找环 Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 117  Solved: 34 Description 给你一副无向图,每条边有 ...

  7. Mouse Hunt CodeForces - 1027D(思维 找环)

    Medicine faculty of Berland State University has just finished their admission campaign. As usual, a ...

  8. HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)

    求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...

  9. Codeforces Beta Round #88 C. Cycle —— DFS(找环)

    题目链接:http://codeforces.com/problemset/problem/117/C C. Cycle time limit per test 2.5 seconds memory ...

随机推荐

  1. MVC和观察者模式

    用观察者模式实现MVC框架! http://wenku.baidu.com/view/eff8bab069dc5022aaea0007.html 写的不错! Observer和ConcreteObse ...

  2. [转]AJAX 跨源 HTTP 请求

    转自OSChina, 原文: http://www.oschina.net/translate/ajax-cross-origin-http-request 背景 跨源HTTP请求(也称跨域AJAX请 ...

  3. malloc calloc realloc 区别

    (1)C语言跟内存分配方式 <1>从静态存储区域分配.       内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量.static变量.<2> ...

  4. CSS控制列表样式属性list-style有哪些?怎么用?

    CSS列表样式属性list-style有哪些类型?不同类型CSS控制列表样式使用时该注意什么? 这是W3Cschool用户Shirley于2016-11-10在W3Cschool编程问答提出的问题.云 ...

  5. redis—Spring中redis缓存的简单使用

    这里使用的是 Spring-4.3 , redis-2.8 的版本   1.添加maven依赖 <dependency> <groupId>redis.clients</ ...

  6. 通过前端控制器源码分析springmvc的执行过程

    第一步:前端控制器接收请求调用doDiapatch 第二步:前端控制器调用处理器映射器查找 Handler 第三步:调用处理器适配器执行Handler,得到执行结果ModelAndView 第四步:视 ...

  7. ZOJ Monthly, January 2018 Solution

    A - Candy Game 水. #include <bits/stdc++.h> using namespace std; #define N 1010 int t, n; int a ...

  8. hdu5107 线段树

    hdu 5107 这题说的是给了一个二维的 平面, 平面内有30000个点每个点都有自己的高度,然后又30000次的查询,每次查询给的是(X,Y,K), 要求出set(x,y){x,y|x<=X ...

  9. 20145322何志威《网络对抗》逆向及Bof基础

    20145322何志威<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任 ...

  10. Linux网络子系统之---- PHY 配置

    MII即媒体独立接口,也叫介质无关接口. 它包括一个数据接口,以及一个MAC和PHY之间的管理接口(图1). 数据接口包括分别用于发送器和接收器的两条独立信道.每条信道都有自己的数据.时钟和控制信号. ...