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 ...
随机推荐
- 大神给你分析HTTPS和HTTP的区别(转)
http://www.php100.com/html/it/biancheng/2015/0209/8582.html 今天在做雅虎的时候,发现用第三方工具截取不到客户端与服务端的通讯,以前重来没碰到 ...
- codevs1183 泥泞的道路(01分数规划)
1183 泥泞的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description CS有n个小区,并且任意小区之间都有两 ...
- oracle-数据库泵EXPDP导出用户下所有
1登录sys用户 2创建目录 create directory [dirname] as ‘[dirpath]’; dirname:取的名字 dirpath:dmp文件导出路径 示例:create d ...
- ASP.NET MVC 导出CSV文件
ASP.NET MVC 导出CSV文件.直接贴代码 /// <summary> /// ASP.NET MVC导出CSV文件Demo1 /// </summary> /// ...
- POJ 2187 凸包+旋转卡壳
思路: 求个凸包 旋转卡壳一下 就求出来最远点对了 注意共线情况 也就是说 凸包如果有一堆点共线保留端点即可 //By SiriusRen #include <cmath> #incl ...
- 全面学习ORACLE Scheduler特性(1)创建jobs
所谓出于job而胜于job,说的就是Oracle 10g后的新特性Scheduler啦.在10g环境中,ORACLE建议使用Scheduler替换普通的job,来管理任务的执行.其实,将Schedul ...
- Roslyn导致发布网站时报错:编译失败
最近新升级了Visual Studio 2017,创建的Web项目Bin目录中多了一个叫roslyn的文件夹,该文件夹导致网站在某些服务器上发布出错 从网上搜索了一下,Roslyn是新出的动态编译工具 ...
- Xml学习笔记(2)
不同的xml文档构可能要用到不同的方法进行解析这里用到的是例如<student name="张三" id="1" sex="男"/&g ...
- NodeJS —— 自定义流的实现
概述 常见的自定义流有四种,Readable(可读流).Writable(可写流).Duplex(双工流)和 Transform(转换流),常见的自定义流应用有 HTTP 请求.响应,crypto 加 ...
- scroll的应用
jQuery(document).ready(function($){ $('#shang').click(function(){ $('html,body').animate({scrollTop: ...