Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible attack by hackers on a major computer network.

In this network are n vertices, some pairs of vertices are connected by m undirected channels. It is planned to transfer q important messages via this network, the i-th of which must be sent from vertex si to vertex di via one or more channels, perhaps through some intermediate vertices.

To protect against attacks a special algorithm was developed. Unfortunately it can be applied only to the network containing directed channels. Therefore, as new channels can't be created, it was decided for each of the existing undirected channels to enable them to transmit data only in one of the two directions.

Your task is to determine whether it is possible so to choose the direction for each channel so that each of the q messages could be successfully transmitted.

  人生中第一道div 1 E题,值得纪念一下。

  题目就是给一个无向图,然后给每条边一个方向,问能不能满足Q个条件,每个条件就是能不能从ai到bi。

  其实不难的题目,然而没有想到双连通卡了一段时间。

  双连通的子图一定能够构造出使其满足任意两点都能互相到,所以先把原图缩点成树。

  树的话方法是对于每个条件 a->b,c=lca(a,b),然后给a-c线段的端点复制+1 -1, c-b同样,但是是维护的另一个数组。

  这样如果对于一个点维护的两个值都为正,说明不可能,否则可以。

  但是注意图可能不连通,12组数据就是这样的,被坑了一次。

代码比较乱,如下:

// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年10月11日 星期日 22时40分22秒
// File Name : B.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=;
const int MaxM=MaxN<<; ////////////////////////////////////////////////////// namespace TU
{ struct Edge
{
int u;
int to,next;
}; Edge E[MaxM];
int head[MaxN],Ecou;
int LOW[MaxN],DFN[MaxN];
int couBridge;
bool bridge[MaxM],cutp[MaxN];
int add_block[MaxN]; // The number of bolcks that is added by deleting a point.(not the number of blocks of this new graph!!!)
int Index; struct newEdge
{
int u,v,num; newEdge(int _u=,int _v=,int _num=):u(_u),v(_v),num(_num) {}; bool operator < (const newEdge &a) const
{
if(u==a.u)
return v<a.v; return u<a.u;
} bool operator == (const newEdge &a) const
{
return u==a.u && v==a.v;
}
}; bool chongE[MaxM]; // Parallel edge.
newEdge remE[MaxM]; void Tarjan(int u,int pre)
{
int v;
int couSon=; LOW[u]=DFN[u]=++Index; for(int i=head[u];i!=-;i=E[i].next)
{
v=E[i].to; if(v==pre) // !!!
continue; if(!DFN[v])
{
++couSon;
Tarjan(v,u); if(LOW[v]<LOW[u])
LOW[u]=LOW[v]; if(DFN[u]<LOW[v] && !chongE[i])
{
bridge[i]=;
bridge[i^]=; //
++couBridge;
}
if(u!=pre && DFN[u]<=LOW[v])
{
cutp[u]=;
++add_block[u];
}
}
else if(DFN[v]<LOW[u])
LOW[u]=DFN[v];
} if(u==pre && couSon>)
{
cutp[u]=;
add_block[u]=couSon-;
}
} void getCUTP(int n)
{
sort(remE,remE+Ecou); for(int i=;i<Ecou;++i)
if(remE[i]==remE[i-])
chongE[remE[i].num]=chongE[remE[i-].num]=; for(int i=;i<=n;++i)
if(!DFN[i])
Tarjan(i,i);
} void addEdge(int u,int v)
{
E[Ecou].u=u;
E[Ecou].to=v;
E[Ecou].next=head[u];
bridge[Ecou]=; chongE[Ecou]=;
remE[Ecou]=newEdge(u,v,Ecou); head[u]=Ecou++;
} void init(int n)
{
Ecou=couBridge=Index=; for(int i=;i<=n;++i)
{
head[i]=-;
cutp[i]=DFN[i]=add_block[i]=;
}
} }; ////////////////////////////////// int belong[MaxN];
int vis[MaxN];
int N;
int TN;
int Q; namespace TREE
{ const int LOG=; struct Edge
{
int next,to;
}; Edge E[MaxN*];
int head[MaxN],Ecou; int dep[MaxN];
int par[MaxN][LOG]; void init()
{
memset(head,-,sizeof(head));
Ecou=;
} void addEdge(int u,int v)
{
E[Ecou].next=head[u];
E[Ecou].to=v;
head[u]=Ecou++;
} int que[MaxN];
int first,last; void BFS(int root,int VI)
{
int t,v; first=last=;
dep[root]=;
par[root][]=root;
que[last++]=root; vis[root]=VI; while(last-first)
{
t=que[first++]; for(int i=;i<LOG;++i)
par[t][i]=par[par[t][i-]][i-]; for(int i=head[t];i!=-;i=E[i].next)
{
v=E[i].to; if(v==par[t][])
continue; dep[v]=dep[t]+;
par[v][]=t;
que[last++]=v; vis[v]=VI;
}
}
} int query(int u,int v)
{
if(dep[u]<dep[v])
swap(u,v); for(int det=dep[u]-dep[v],i=;det;det>>=,++i)
if(det&)
u=par[u][i]; if(u==v)
return u; for(int i=LOG-;i>=;--i)
if(par[u][i]!=par[v][i])
{
u=par[u][i];
v=par[v][i];
} return par[u][];
} // ------------- int rem1[MaxN],rem2[MaxN];
int sum1[MaxN],sum2[MaxN];
bool tvis[MaxN]; bool dfs111(int u,int pre)
{
tvis[u]=; sum1[u]=rem1[u];
sum2[u]=rem2[u]; for(int i=head[u];i!=-;i=E[i].next)
if(E[i].to!=pre)
{
if(dfs111(E[i].to,u)==) return ;
sum1[u]+=sum1[E[i].to];
sum2[u]+=sum2[E[i].to];
} if(sum1[u]> && sum2[u]> && pre!=-) return ;
return ;
} bool judge()
{
int a,b;
int c;
int VI=; for(int i=;i<=N;++i)
if(!vis[i])
BFS(i,++VI); while(Q--)
{
scanf("%d %d",&a,&b);
a=belong[a];
b=belong[b]; if(vis[a]!=vis[b]) return ; if(a!=b)
{
c=query(a,b);
++rem1[a];
--rem1[c];
++rem2[b];
--rem2[c];
}
} for(int i=;i<=N;++i)
if(tvis[i]==)
if(!dfs111(i,-))
return ;
return ;
} }; /////////////////////////////////////// int que[MaxN];
int first,last; void bfs(int u,int be)
{
first=last=;
que[last++]=u;
belong[u]=be; while(last-first)
{
u=que[first++];
for(int i=TU::head[u];i!=-;i=TU::E[i].next)
if(TU::bridge[i]== && belong[TU::E[i].to]==)
{
belong[TU::E[i].to]=be;
que[last++]=TU::E[i].to;
}
}
} void getbelong()
{
N=;
for(int i=;i<=TN;++i)
if(!belong[i])
bfs(i,++N);
} void addEdge()
{
for(int i=;i<TU::Ecou;++i)
if(TU::bridge[i])
TREE::addEdge(belong[TU::E[i].u],belong[TU::E[i].to]);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int M;
int a,b; scanf("%d %d %d",&TN,&M,&Q); TU::init(TN);
while(M--)
{
scanf("%d %d",&a,&b);
TU::addEdge(a,b);
TU::addEdge(b,a);
}
TU::getCUTP(TN); getbelong(); TREE::init();
addEdge(); if(TREE::judge()) puts("Yes");
else puts("No"); return ;
}

(中等) CF 555E Case of Computer Network,双连通+树。的更多相关文章

  1. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  2. 555E Case of Computer Network

    分析 一个连通块内的肯定不影响 于是我们先缩点 之后对于每个路径 向上向下分别开一个差分数组 如果两个数组同时有值则不合法 代码 #include<bits/stdc++.h> using ...

  3. 「CF555E」 Case of Computer Network

    「CF555E」 Case of Computer Network 传送门 又是给边定向的题目(马上想到欧拉回路) 然而这个题没有对度数的限制,你想歪了. 然后又开始想一个类似于匈牙利的算法:我先跑, ...

  4. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  5. Solution -「CF 555E」Case of Computer Network

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个点 \(m\) 条边的无向图,判断是否有给每条边定向的方案,使得 \(q\) 组有序点对 \((s,t)\) ...

  6. CF555E Case of Computer Network

    题面:https://www.luogu.com.cn/problem/CF555E 题意:给定一张\(n\)个点\(m\)条边的无向图. 给定\(q\)组有向点对\((s,t)\). 询问是否存在使 ...

  7. 题解 CF555E Case of Computer Network

    题目传送门 题目大意 给出一个\(n\)个点\(m\)条边的无向图,有\(q\)次有向点对\((s,t)\),问是否存在一种方法定向每条边使得每个点对可以\(s\to t\). \(n,m,q\le ...

  8. [J]computer network tarjan边双联通分量+树的直径

    https://odzkskevi.qnssl.com/b660f16d70db1969261cd8b11235ec99?v=1537580031 [2012-2013 ACM Central Reg ...

  9. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

随机推荐

  1. PHP之输出控制 ob_start(),ob_get_contents(),ob_end_clean()

    1.常用函数 ob_start();#打开输出缓冲区 ob_get_contents();#获取缓冲区内容 ob_get_length();#获取缓冲区内容长度 ob_clean();#清除之前的所有 ...

  2. vs找不到svn源代码管理插件之我见

    使用svn要安装两个文件,一个客户端:TortoiseSVN-1.8.msi,一个插件:AnkhSvn-2.5.msi:两个都安装好之后,在vs的tool(工具)选项卡中,选择自定义,然后选择sour ...

  3. Inno Setup入门(十八)——Inno Setup类参考(4)

    分类: Install Setup 2013-02-02 11:29 406人阅读 评论(0) 收藏 举报 编辑框 编辑框也叫文本框,是典型的窗口可视化组件,既可以用来输入文本,也可以用来显示文本,是 ...

  4. Inno Setup入门(十四)——替换安装程序和卸载程序的图标

    通常Inno生成的安装文件的图标是一个光盘和显示器,如下图.同时,程序安装好之后,在安装目录下的卸载程序的图标也是一样的,其实我们也可以自己修改. 首先生成的安装文件图标.这个比较简单,只需要在Set ...

  5. Inno Setup入门(二)——修改安装过程中的图片

    修改安装过程中的图片 一般编译之后,安装过程中出现在左边图片是是下图这个样子的: 其实也可以修改它,只需要在setup段中作一点稍微的修改,加一行代码即可: [setup] AppName=Test ...

  6. grub修复

    sudo mount /dev/sda1 /boot/efi sudo modprobe efivarfs sudo grub-install /dev/sda sudo update-grub su ...

  7. VS2010中安装AjaxControlToolkit

    原文地址:http://www.asp.net/ajaxlibrary/act.ashx 第一步 下载Ajax Control Toolkit 进入网址http://ajaxcontroltoolki ...

  8. DISUBSTR - Distinct Substrings

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  9. HDU 3732 Ahui Writes Word 多重背包优化01背包

    题目大意:有n个单词,m的耐心,每个单词有一定的价值,以及学习这个单词所消耗的耐心,耐心消耗完则,无法学习.问能学到的单词的最大价值为多少. 题目思路:很明显的01背包,但如果按常规的方法解决时间复杂 ...

  10. Android实现播放GIF动画的强大ImageView

    我个人是比较喜欢逛贴吧的,贴吧里总是会有很多搞笑的动态图片,经常看一看就会感觉欢乐很多,可以释放掉不少平时的压力.确实,比起一张单调的图片,动态图片明显更加的有意思.一般动态图片都是GIF格式的,浏览 ...