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 ...
随机推荐
- Ruby实例方法和类方法的简写
创建: 2017/12/12 类方法 Sample.func 实例方法 Sample#func
- 清北考前刷题da5下午好
/* (4,1)*(3,1)*(2,1)的话1变成2然后一直是2 2变成1然后变成3 3变成1然后变成4 4变成1 */ #include<iostream> #include<cs ...
- 键盘按钮keyCode大全:获取按键对应的键值的方法
没有大全,只有方法,授人与鱼不如授人于渔: 下面这行代码,大家可以打在控制台里,直接进行测试: document.body.onkeyup = function (e) { e = e || wind ...
- nginx部署vue项目 解决方案
给前端同事部署了web项目之后,访问发现除了index.html可以访问,其他的路径使用了“伪静态”.比如访问:http://localhost:8081/user/login,访问即报404错误,这 ...
- PowerDesigner 的使用教程
PowerDesigner 的使用这两篇博客挺好,我也是跟着学习,就不再写了: 初步学习: http://www.cnblogs.com/huangcong/archive/2010/06/14/17 ...
- 系统资源监控--windows
前言: 系统资源监控一般监控系统的CPU,内存,磁盘和网络.系统分为windows和Linux.本篇主要记录windows. Windows的监控相对与Linux监控工具来说比较简单,更多的是查看wi ...
- Linux系统调用--getrusage函数详解
Linux系统调用--getrusage函数详解 功能描述: 获得进程的相关资源信息.如:用户开销时间,系统开销时间,接收的信号量等等; 用法: #include <sys/t ...
- jsp学习笔记 - 内置对象 pageContext
1.pageContext几乎可以操作所有的页面内置对象 pageContext.getRequest(); 得到的对象只是属于ServletRequest类,httpServletReques ...
- POJ_1611_The Suspect
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 25149 Accepted: 12329 De ...
- Change the color of a link in an NSMutableAttributedString
Swift Updated for Swift 3 Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: ...