hdu 4598 Difference

Difference

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

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

2013 ACM-ICPC吉林通化全国邀请赛——题目重现

给你一个图,问是不是满足题目要求的条件,

|a[i]-a[j]| >= T ;对于在图里的边,不在图里的边|a[i]-a[j]| < T

| a[i] | < T ;

我们很容易知道如果 i-j有边,a[i],a[j]符号肯定不同,那么这个就是二分图了,染色法可以做

如果可以染色 我们可以随便取个 T 值。然后构造差分约束方程,

跑最短路判负圈

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define mod 1000000007
#define maxn 410
#define pi acos(-1.0)
#define eps 1e-8
#define INF 0x3f3f3f3f
using namespace std;

vector<int>qe[maxn] ;
int color[maxn] ,flag,top,val[maxn*maxn] ;
int head[maxn],to[maxn*maxn],next1[maxn*maxn];
char a[maxn][maxn] ;

void Unit(int u,int v,int c)
{
    next1[top] = head[u] ;to[top] = v ;
    val[top]=c;head[u]=top++;
}
void dfs(int u,int fa,int c )
{
    color[u]=c;
    for(int i = 0 ; i < qe[u].size();i++)
    {
        int v = qe[u][i] ;
        if(v==fa) continue ;
        if(color[v]==-1)
        {
            dfs(v,u,1-c) ;
        }
        else if(color[v]==c)
        {
            flag=1;
            return ;
        }
    }
}
int dis[maxn] ,cnt[maxn];
bool vi[maxn];
bool spfa(int s,int n)
{
    queue<int>q;
    for(int i = 1 ; i <= n ;i++)
    {
        dis[i]=0;
        vi[i]=1;
        q.push(i);
        cnt[i]=0;
    }
    int i,u,v;
    while(!q.empty())
    {
        u  = q.front();q.pop();
        for( i = head[u] ; i != -1 ; i = next1[i])
        {
            v = to[i] ;
            if(dis[v] > dis[u]+val[i])
            {
                dis[v]=dis[u]+val[i] ;
                if(!vi[v])
                {
                    vi[v]=true;
                    cnt[v]++;
                    if(cnt[v]>n) return false;
                    q.push(v) ;
                }
            }
        }
        vi[u]=false;
    }
    return true;
}
int main()
{
    int j,i,l,g,n;
    int T,ans1,u,v;
    cin >> T ;
    while(T--)
    {
        scanf("%d",&n) ;
        for( i = 1 ; i <= n ;i++){
            scanf("%s",a[i]+1) ;
            qe[i].clear();
        }
        for( i = 1 ; i <= n ;i++)
            for( j = 1 ; j <= n ;j++)if(a[i][j]==‘1‘){
                qe[i].push_back(j);
            }
        memset(color,-1,sizeof(color)) ;
        flag=0;
        for( i = 1 ; i <= n ;i++)if(color[i]==-1){
            dfs(i,-1,1) ;
            if(flag) break ;
        }
        if(flag){
            puts("No") ;
            continue ;
        }
        top=0;
        memset(head,-1,sizeof(head)) ;
        for( i = 1 ; i <= n ;i++)
            for( j = i+1 ; j <= n ;j++)
        {
            if(a[i][j]==‘1‘)
            {
                if(color[i])
                 Unit(i,j,-maxn) ;
                else Unit(j,i,-maxn) ;
            }
            else
            {
                if(color[i])
                    Unit(j,i,maxn-1);
                else Unit(i,j,maxn-1) ;
            }
        }
        if(spfa(1,n))puts("Yes") ;
        else puts("No") ;
    }
    return 0 ;
}

时间: 2024-12-15 23:12:43

hdu 4598 Difference的相关文章

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

Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 62    Accepted Submission(s): 19 Problem Description Little Ruins is playing a number game, first he chooses two positive integers y an

Hdu 4594 Difference(奇圈判断+差分约束)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4598 思路:由题意可知两相连点ai符号一定相反,所以若存在奇圈则一定无解.染色,colour[i]==1表示为正,colour[i]==2表示为负.由于(b)条件为充要条件,所以对于图中的点| a[i]-a[j] | >= T,对于非图中点| a[i]-a[j] | < T,即| a[i]-a[j] | <= T-1 .所以图中点,若colour[i]==1,a[i]-a[j] >=

HDU 5486 Difference of Clustering 图论

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5486 题意: 给你每个元素一开始所属的集合和最后所属的集合,问有多少次集合的分离操作,并操作和不变操作. 分离:[m1,m2,m3]->[m1],[m2],[m3] 合并:分离的逆操作 不变:[m1,m2,m3]->[m1,m2,m3] 题解: 以集合为单位建图,(一个元素从集合s1到s2则建一条边连接集合s1,s2,注意要删除重边) 然后对于每个点,与它相邻的点如果入度都为1,则为分离操作,

HDU 4715 Difference Between Primes (素数表+二分)

Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2998    Accepted Submission(s): 850 Problem Description All you know Goldbach conjecture.That is to say, Every even inte

HDU 5936 Difference(折半搜索(中途相遇法))

http://acm.hdu.edu.cn/showproblem.php?pid=5936 题意: 定义了这样一种算法,现在给出x和k的值,问有多少个y是符合条件的. 思路: y最多只有10位,再多x就是负的了. 这样的话可以将y分为前后两部分,我们先枚举后5位的情况,然后再枚举前5位的情况,通过二分查找找到匹配的项,这样就大大的降低了时间复杂度. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring&g

hdu 4715 Difference Between Primes (二分查找)

Problem Description All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conjecture: every even integer can be expressed as the difference of two pr

模拟2

    ID Origin Title 6 / 10 Problem A HDU 4706 Children's Day 7 / 22 Problem B HDU 4707 Pet 5 / 25 Problem C HDU 4708 Rotation Lock Puzzle 6 / 26 Problem D HDU 4709 Herding 1 / 9 Problem E HDU 4710 Balls Rearrangement     Problem F HDU 4711 Weather  

补题列表

上海网络赛: HDU 5468 Puzzled Elena HDU 5469 Antonidas HDU 5473 There was a kingdom 合肥网络赛: HDU 5487 Difference of Languages HDU 5486 Difference of Clustering HDU 5489 Removed Interval HDU 5492 Find a path HDU 5493 Queue 弱校联萌Day1: B. Carries D. Vertex Cover

HDU 3161 Iterated Difference 暴力

Iterated Difference Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 786    Accepted Submission(s): 505 Problem Description You are given a list of N non-negative integers a(1), a(2), ... , a(N).