判断割是否唯一zoj2587
Time Limit: 5 Seconds Memory Limit: 32768 KB
N supercomputers in the United States of Antarctica are connected into a network. A network has a simple topology: M different pairs of supercomputers are connected to each other by an optical fibre. All connections are two-way, that is, they can be used in both directions. Data can be transmitted from one computer to another either directly by a fibre, or using some intermediate computers.
A group of terrorists is planning to attack the network. Their goal is to separate two main computers of the network, so that there is no way to transmit data from one of them to another. For each fibre the terrorists have calculated the sum of money they need to destroy the fibre. Of course, they want to minimize the cost of the operation, so it is required that the total sum spent for destroying the fibres was minimal possible.
Now the leaders of the group wonder whether there is only one way to do the selected operation. That is, they want to know if there are no two different sets of fibre connections that can be destroyed, such that the main supercomputers cannot connect to each other after it and the cost of the operation is minimal possible.
Input
The input file consists of several cases. In each case, the first line of the input file contains N, M, A and B (2 <= N <= 800, 1 <= M <= 10000, 1 <= A,B <= N, A != B), specifying the number of supercomputers in the network, the number of fibre connections, and the numbers of the main supercomputers respectively. A case with 4 zeros indicates the end of file.
Next M lines describe fibre connections. For each connection the numbers of the computers it connects are given and the cost of destroying this connection. It is guaranteed that all costs are non-negative integer numbers not exceeding 105, no two computers are directly connected by more than one fibre, no fibre connects a computer to itself and initially there is the way to transmit data from one main supercomputer to another.
Output
If there is only one way to perform the operation, output "UNIQUE" in a single line. In the other case output "AMBIGUOUS".
Sample Input
- 4 4 1 2
- 1 2 1
- 2 4 2
- 1 3 2
- 3 4 1
- 4 4 1 2
- 1 2 1
- 2 4 1
- 1 3 2
- 3 4 1
- 0 0 0 0
Sample Output
- UNIQUE
- AMBIGUOUS
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int N=;
- int head[N],tot,S,T;
- int q[N],dis[N],n,m;
- bool vis[N];
- struct node
- {
- int next,v,w;
- } e[N*];
- void add(int u,int v,int w)
- {
- e[tot].v=v;
- e[tot].w=w;
- e[tot].next=head[u];
- head[u]=tot++;
- }
- bool bfs()
- {
- memset(dis,-,sizeof(dis));
- dis[S]=;
- int l=,r=;
- q[r++]=S;
- while(l<r)
- {
- int u=q[l++];
- for(int i=head[u]; ~i; i=e[i].next)
- {
- int v=e[i].v;
- if(dis[v]==-&&e[i].w>)
- {
- q[r++]=v;
- dis[v]=dis[u]+;
- if(v==T) return true;
- }
- }
- }
- return false;
- }
- int dfs(int s,int low)
- {
- if(s==T||!low) return low;
- int ans=low,a;
- for(int i=head[s]; ~i; i=e[i].next)
- {
- if(e[i].w>&&dis[e[i].v]==dis[s]+&&(a=dfs(e[i].v,min(e[i].w,ans))))
- {
- e[i].w-=a;
- e[i^].w+=a;
- ans-=a;
- if(!ans) return low;
- }
- }
- if(low==ans) dis[s]=-;
- return low-ans;
- }
- void dfs(int &cnt,int u,int k){
- for(int i=head[u];~i;i=e[i].next){
- int v=e[i].v;
- if(!vis[v]&&e[i^k].w) {
- vis[v]=;
- ++cnt;
- dfs(cnt,v,k);
- }
- }
- }
- int main(){
- int x,y,z;
- while(scanf("%d%d%d%d",&n,&m,&S,&T)!=EOF){
- if(n+m+S+T==) break;
- memset(head,-,sizeof(head));
- tot=;
- while(m--){
- scanf("%d%d%d",&x,&y,&z);
- add(x,y,z);
- add(y,x,z);
- }
- while(bfs()) dfs(S,);
- int cnt1=,cnt2=;
- memset(vis,,sizeof(vis));
- dfs(cnt1,S,);
- memset(vis,,sizeof(vis));
- dfs(cnt2,T,);
- if(cnt1>) --cnt1;
- if(cnt2>) --cnt2;
- printf("%s\n",cnt1+cnt2==n?"UNIQUE":"AMBIGUOUS");
- }
- }
判断割是否唯一zoj2587的更多相关文章
- ZOJ - 2587 Unique Attack (判断最小割是否唯一)
题意:判断最小割是否唯一. 分析:跑出最大流后,在残余网上从源点和汇点分别dfs一次,对访问的点都打上标记. 若还有点没有被访问到,说明最小割不唯一. https://www.cnblogs.com/ ...
- POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)
题意:给出n个字符,m对关系,让你输出三种情况: 1.若到第k行时,能判断出唯一的拓扑序列,则输出: Sorted sequence determined after k re ...
- poj 1679 判断MST是不是唯一的 (次小生成树)
判断MST是不是唯一的 如果是唯一的 就输出最小的权值和 如果不是唯一的 就输出Not Unique! 次小生成树就是第二小生成树 如果次小生成树的权值和MST相等 那么MST就不是唯一的 法一: ...
- POJ 1679 The Unique MST(判断最小生成树是否唯一)
题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...
- poj1679(判断最小生成树是否唯一)
题意:给出n个点,m条边,要你判断最小生成树是否唯一. 思路:先做一次最小生成树操作,标记选择了的边,然后枚举已经被标记了的边,判断剩下的边组成的最小生成树是否与前面的相等,相等,则不唯一,否则唯一. ...
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一
Pre- and Post-order Traversals PAT-1119 这题难度较大,主要需要考虑如何实现根据前序遍历和后序遍历来确定一颗二叉树 一篇好的文章: 题解 import java. ...
- POJ 1679 The Unique MST 【判断最小生成树是否唯一】
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Defini ...
随机推荐
- mysql 之 清空表中数据
清空表的时候注意外键约束 命令版 查询数据库中所有表名select table_name from information_schema.tables where table_schema='DB_n ...
- iOS架构入门 - MVC模式实例演示
MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并且使程序某一部分的重复利用成为可能.除此之外,此模式通过对复杂度的简化,使程序结构更加直观 控制器(Controller)-- ...
- Python开源框架总结
Django: Python Web应用开发框架 Django 应该是最出名的Python框架,GAE甚至Erlang都有框架受它影响.Django是走大而全的方向,它最出名的是其全自动化的管理后台: ...
- USACO 2.1 海明码 Hamming Codes (模拟+位运算+黑科技__builtin_popcount(n))
题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位 ...
- Java面试题:抽象类和接口的区别
1.abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系.但是,一个类却可以实现多个interface. 2.在abstract class 中可以有自己 ...
- JMeter Nmon Tool V2.0 插件
很早之前宝路已将nmon监控功能集成到了JMeter中,自己在使用旧版本时,也有诸多不满意的地方.趁着五一假期(基本都是晚上,白天要陪孩子),对插件底层代码进行了重构,自己还要反复测试调整,最晚的一次 ...
- redis系列之2----详细讲解redis数据结构(内存模型)以及常用命令
Redis数据类型 与Memcached仅支持简单的key-value结构的数据记录不同,Redis支持的数据类型要丰富得多,常用的数据类型主要有五种:String.List.Hash.Set和Sor ...
- SpringBoot集成Quartz实现定时任务
1 需求 在我的前后端分离的实验室管理项目中,有一个功能是学生状态统计.我的设计是按天统计每种状态的比例.为了便于计算,在每天0点,系统需要将学生的状态重置,并插入一条数据作为一天的开始状态.另外,考 ...
- c/c++获取硬盘序列号
最近在接触软件注册模块,需要获取硬盘序列号来生成注册码. 硬盘序列号,英文名:Hard Disk Serial Number,该号是硬盘厂家为区别产品而设置的,是唯一的.网上搜索一下,发现获取硬盘序列 ...
- OSG程序设计之Hello World 4.0
代码如下: //需要添加两个库:osgUtild.lib.osgTextd.lib #include <osgDB/ReadFile> #include <osgUtil/Optim ...