Difference

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 581    Accepted Submission(s): 152

Problem Description
A graph is a difference if every vertex vi can be assigned a real number ai and there exists a positive real number T such that
(a) |ai| < T for all i and 
(b) (vi, vj) in E <=> |ai - aj| >= T,
where E is the set of the edges. 
Now given a graph, please recognize it whether it is a difference.
 
Input
The first line of input contains one integer TC(1<=TC<=25), the number of test cases.
Then TC test cases follow. For each test case, the first line contains one integer N(1<=N<=300), the number of vertexes in the graph. Then N lines follow, each of the N line contains a string of length N. The j-th character in the i-th line is "1" if (vi, vj) in E, and it is "0" otherwise. The i-th character in the i-th line will be always "0". It is guaranteed that the j-th character in the i-th line will be the same as the i-th character in the j-th line.
 
Output
For each test case, output a string in one line. Output "Yes" if the graph is a difference, and "No" if it is not a difference.
 
Sample Input
3
4
0011
0001
1000
1100
4
0111
1001
1001
1110
3
000
000
000
 
Sample Output
Yes
No
Yes
 
Hint

In sample 1, it can let T=3 and a[sub]1[/sub]=-2, a[sub]2[/sub]=-1, a[sub]3[/sub]=1, a[sub]4[/sub]=2.

 
Source
 
Recommend
liuyiding
 
解题:差分约束,完全不懂为什么要这样子。学渣的无奈啊
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ,T = ;
struct arc{
int u,v,w;
arc(int x = ,int y = ,int z = ){
u = x;
v = y;
w = z;
}
};
vector<int>g[maxn];
vector<int>G[maxn];
vector<arc>e;
char mp[maxn][maxn];
int n,color[maxn],d[maxn],cnt[maxn];
bool in[maxn];
void dfs(int u,int c){
color[u] = c;
for(int i = ; i < g[u].size(); i++)
if(!color[g[u][i]]) dfs(g[u][i],-c);
}
void add(int u,int v,int w){
e.push_back(arc(u,v,w));
G[u].push_back(e.size()-);
}
bool spfa(){
queue<int>q;
for(int i = ; i < n; i++){
d[i] = ;
cnt[i] = ;
in[i] = true;
q.push(i);
}
while(!q.empty()){
int u = q.front();
q.pop();
in[u] = false;
for(int i = ; i < G[u].size(); i++){
arc &temp = e[G[u][i]];
if(d[temp.v] > d[u]+temp.w){
d[temp.v] = d[u]+temp.w;
if(!in[temp.v]){
in[temp.v] = true;
cnt[temp.v]++;
if(cnt[temp.v] > n) return true;
q.push(temp.v);
}
}
}
}
return false;
}
bool solve(){
for(int i = ; i < n; i++) if(!color[i]) dfs(i,);
for(int i = ; i < n; i++)
for(int j = ; j < g[i].size(); j++)
if(color[i] == color[g[i][j]]) return false;
for(int i = ; i < n; i++){
for(int j = i+; j < n; j++){
if(mp[i][j] == '')
color[i] == ?add(i,j,-T):add(j,i,-T);
else color[i] == ?add(j,i,T-):add(i,j,T-);
}
}
return !spfa();
}
int main() {
int ks,i,j;
scanf("%d",&ks);
while(ks--){
scanf("%d",&n);
for(i = ; i <= n; i++){
g[i].clear();
G[i].clear();
color[i] = ;
}
e.clear();
for(i = ; i < n; i++){
scanf("%s",mp[i]);
for(j = ; j < n; j++)
if(mp[i][j] == '') g[i].push_back(j);
}
solve()?puts("Yes"):puts("No");
}
return ;
}
 
 
 

HDU 4598 Difference的更多相关文章

  1. hdu 4598 Difference(奇圈判定+差分约束)

    这是通化邀请赛的题,当时比赛的时候还完全没想法呢,看来这几个月的训练还是有效果的... 题意要求(1) |ai| < T for all i   (2) (vi, vj) in E <=& ...

  2. HDU 5487 Difference of Languages(BFS)

    HDU 5487 Difference of Languages 这题从昨天下午2点开始做,到现在才AC了.感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能 ...

  3. hdu 4715 Difference Between Primes

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...

  4. HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))

    Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. HDU 5487 Difference of Languages

    Difference of Languages Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. ...

  6. HDU 5486 Difference of Clustering 暴力模拟

    Difference of Clustering HDU - 5486 题意:有n个实体,新旧两种聚类算法,每种算法有很多聚类,在同一算法里,一个实体只属于一个聚类,然后有以下三种模式. 第一种分散, ...

  7. HDU 5486 Difference of Clustering 图论

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5486 题意: 给你每个元素一开始所属的集合和最后所属的集合,问有多少次集合的分离操作,并操作和不变操 ...

  8. hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...

  9. HDU 4715 Difference Between Primes (打表)

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. JS 封装插件

    媒体播放器插件: mediaelement-and-player.js 轮播图插件: swiper.min.js

  2. ASP.NET MVC5 之 分部页

    1.分部页 _PartialPage.cshtml @model List<string> <a>完美世界</a> @foreach (var item in Mo ...

  3. 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game

    题目传送门 /* 贪心:暴力贪心水水 */ #include <cstdio> #include <algorithm> #include <cstring> us ...

  4. 405 Convert a Number to Hexadecimal 数字转换为十六进制数

    给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意:    十六进制中所有字母(a-f)都必须是小写.    十六进制字符串中不能包含多余的前导零.如果 ...

  5. ListView用法

    public class MainActivity extends Activity implements OnItemClickListener, OnScrollListener { privat ...

  6. 联想 S5【K520】免解锁BL 免rec 保留数据 Magisk Xposed 救砖 ROOT ZUI 3.7.490

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  7. ElasticSearch学习笔记--一些规范,会持续更新

    我们在ElasticSearch中存储的数据一般是采用json的格式存储,所以ElasticSearch中有一个叫Mapper的东西用来定义jsonschema来规范这个json 但是这个mapper ...

  8. 【C++】朝花夕拾——中缀转后缀

    对于简单的四则运算而言,后缀表达式可以通过使用栈(stack)快速算出结果 ==================================我是分割线======================= ...

  9. Controller传值到前端页面的几种方式

    一丶追加字符串传值 #region 02-追加字符串传值 /// <summary> /// 02-追加字符串传值 /// </summary> /// <returns ...

  10. Python 操作excel day5

    一.Python操作excel python操作excel使用xlrd.xlwt和xlutils模块 1.xlrd模块是读取excel的: 2.xlwt模块是写excel的: 3.xlutils是用来 ...