HDU 4598 Difference
Difference
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 581 Accepted Submission(s): 152
(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.
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.
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.
#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的更多相关文章
- hdu 4598 Difference(奇圈判定+差分约束)
这是通化邀请赛的题,当时比赛的时候还完全没想法呢,看来这几个月的训练还是有效果的... 题意要求(1) |ai| < T for all i (2) (vi, vj) in E <=& ...
- HDU 5487 Difference of Languages(BFS)
HDU 5487 Difference of Languages 这题从昨天下午2点开始做,到现在才AC了.感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能 ...
- hdu 4715 Difference Between Primes
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...
- HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))
Difference Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 5487 Difference of Languages
Difference of Languages Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. ...
- HDU 5486 Difference of Clustering 暴力模拟
Difference of Clustering HDU - 5486 题意:有n个实体,新旧两种聚类算法,每种算法有很多聚类,在同一算法里,一个实体只属于一个聚类,然后有以下三种模式. 第一种分散, ...
- HDU 5486 Difference of Clustering 图论
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5486 题意: 给你每个元素一开始所属的集合和最后所属的集合,问有多少次集合的分离操作,并操作和不变操 ...
- hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...
- HDU 4715 Difference Between Primes (打表)
Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- nrm -- NPM registry 管理工具
https://cnodejs.org/topic/5326e78c434e04172c006826 cnpm:http://npm.taobao.org/
- CodeForces 731C Socks (DFS或并查集)
题意:有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色. 析:很明显,每个连通块都必须是同一种颜色,然后再统计最多颜色的就好了,即可以用并查集也可以用DFS ...
- Shell脚本下条件测试(eq.ne.....)(转载)
转载:http://cxj632840815.blog.51cto.com/3511863/1168709 Shell编程中的条件测试 在Linux编程中经常会用到判断数值的大小,字符串是否为空这样或 ...
- 使用Redis存储Nginx+Tomcat负载均衡集群的Session
配置Tomcat的session共享可以有三种解决方案: 第一种是以负载均衡服务器本身提供的session共享策略,每种服务期的配置是不一样的并且nginx本身是没有的. 第二种是利用web容器本身的 ...
- Manacher HDOJ 5371 Hotaru's problem
题目传送门 /* 题意:求形如(2 3 4) (4 3 2) (2 3 4)的最长长度,即两个重叠一半的回文串 Manacher:比赛看到这题还以为套个模板就行了,因为BC上有道类似的题,自己又学过M ...
- javascript 信息的发布与删除
现在很多类似以微博发布动态的效果,下面为一个用 JavaScript写的小小的类似微博发布信息的案例 <!DOCTYPE html> <html lang="en" ...
- js实现浮动框跟随页面滚动,最后停留在原来位置
左边悬浮的二维码会跟随页面向上或者向下滚动,最后停留在原来的位置. <div style="background:red; width:1000px; height:7000px; m ...
- 短URL生成
算法原理 算法一 1)将长网址md5生成32位签名串,分为4段, 每段8个字节; 2)对这四段循环处理, 取8个字节, 将他看成16进制串与0x3fffffff(30位1)与操作, 即超过30位的忽略 ...
- poi获取word批注
package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- Git——github高级
分支管理 分支不是越多越好,只求一个稳定的分支,即master不要轻易去更改 对应master要有一个开发者分支,保证mater分支的稳定性 所有的功能都在开发者分支上进行 在所有功能开发后新建发布分 ...