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. sqlserver2005连接失败,不存在或拒绝访问

    sqlserver2005连接失败,不存在或拒绝访问 启动tcp/ip连接的方法: 打开 /Microsoft SQL Server 2005/配置工具/目录下的SQL Server Configur ...

  2. Bug分支(转载)

    转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137602359178 ...

  3. [App Store Connect帮助]六、测试 Beta 版本(4.4) 管理 Beta 版构建版本:停止测试构建版本

    在首页上,点按“我的 App”,选择您的 App,然后在工具栏中点按“TestFlight”. 在左列中的“构建版本”下,点按您 App 的平台(iOS 或 Apple TVOS). 在右表中,点按该 ...

  4. Akka源码分析-Cluster-Metrics

    一个应用软件维护的后期一定是要做监控,akka也不例外,它提供了集群模式下的度量扩展插件. 其实如果读者读过前面的系列文章的话,应该是能够自己写一个这样的监控工具的.简单来说就是创建一个actor,它 ...

  5. RabbitMq安装成功后执行命令报错(Error: unable to connect to node 'rabbit@DESKTOP-LPKSION': nodedown)

    我们直接来看解决方案吧.首先打开服务,找到RabbitMq服务. 双击打开后选择登陆选项卡: 点选此账户,输入你计算机的登录名称.点击浏览: 在这里输入你的用户名,点检索: 这里的密码输入你电脑开机登 ...

  6. Syntax error on token ";", , expected 错误

    eclipse错误提示如图: 错误代码如图: 一开始百思不得其解,后来终于发现问题的原因所在,java中变量的声明可以不在方法中,但语句只能出现在方法中,可以再声明变量的时候就赋初值,但如果要单独赋值 ...

  7. deepin 安装maven

    1.在官网 http://maven.apache.org/download.cgi 下载mvn包   2.新建mvn目录 mkdir  /usr/local/mvn 把压缩包解压到mvn目录下面 修 ...

  8. [LOJ#10064]黑暗城堡

    Description 在顺利攻破 Lord lsp 的防线之后,lqr 一行人来到了 Lord lsp 的城堡下方.Lord lsp 黑化之后虽然拥有了强大的超能力,能够用意念力制造建筑物,但是智商 ...

  9. C#上机作业及代码Question1

    第一题创建控制台应用程序,利用下面的公式计算 q 的近似值,并运行出 k=10 时 q 的值. 本着开源的精神,献给各位,题目简单,随便写写,功能都实现了,不过现在先上传简单题,有一些难度的题目我先留 ...

  10. 383 Ransom Note 赎金信

    给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 true :否则返回 ...