L - Median

博客围绕山东省赛的一道题展开,题目给出元素间大小关系,判断各元素能否为中位数。解题思路是判断大于、小于某元素的数字不能多于一半,利用有向图传递关系,因数据规模不大,采用floyd算法求解,还给出了AC代码。

Description

Recall the definition of the median of nn elements where nn is odd: sort these elements and the median is the (n+1)2(n+1)2-th largest element.
In this problem, the exact value of each element is not given, but mm relations between some pair of elements are given. The ii-th relation can be described as (ai,bi)(ai,bi), which indicates that the aiai-th element is strictly larger than the bibi-th element.
For all 1≤k≤n1≤k≤n, is it possible to assign values to each element so that all the relations are satisfied and the kk-th element is the median of the nn elements?

Input

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:
The first line contains two integers nn and mm (1≤n<1001≤n<100, 1≤m≤n21≤m≤n2), indicating the number of elements and the number of relations. It’s guaranteed that nn is odd.
For the following mm lines, the ii-th line contains two integers aiai and bibi, indicating that the aiai-th element is strictly larger than the bibi-th element. It guaranteed that for all 1≤i<j≤m1≤i<j≤m, ai≠ajai≠aj or bi≠bjbi≠bj.
It’s guaranteed that the sum of nn of all test cases will not exceed 2×1032×103.

Output

For each test case output one line containing one string of length nn. If it is possible to assign values to each element so that all the relations are satisfied and the ii-th element is the median, the ii-th character of the string should be ‘1’, otherwise it should be ‘0’.

Sample Input

2
5 4
1 2
3 2
2 4
2 5
3 2
1 1
2 3

Sample Output

01000
000

Hint

For the first sample test case, as the 2nd element is smaller than the 1st and the 3rd elements and is larger than the 4th and the 5th elements, it’s possible that the 2nd element is the median.For the second sample test case, as the 1st element can’t be larger than itself, it’s impossible to assign values to the elements so that all the relations are satisfied.

题目大意

t为样例数目
每个样例第一行有两个数字n,m表示有1-n个元素,m个关系
每一个关系由两个数字a,b组成表示a元素大于b元素(其实大于小于交不交换都没有影响)输出n个数字表示1-n个元素可不可能为中位数(可能在它的位置上输出1否则输出0)

解题思路

首先判断可不可能是中位数就是要判断大于它的数字不能多于一半小于它的数字也不能多于一半,大于小于关系是可以传递的可以联想到有向图每一条路线表示起点和终点的关系(大于或小于)数据规模也不太大可以用floyd

AC代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int w[105][105],ma[105],mi[105];
int main()
{
    ll t,n,m,a,b;
    cin>>t;
    while(t--)
    {
        int vis=0;
        cin>>n>>m;
        memset(w,0,sizeof(w));
        memset(mi,0,sizeof(mi));
        memset(ma,0,sizeof(ma));
        for(int i=0; i<m; ++i)
        {
            cin>>a>>b;
            if(a==b)
                vis=1;
            w[a][b]=1;
        }
        for(int k=1; k<=n; ++k)
            for(int i=1; i<=n; ++i)
                for(int j=1; j<=n; ++j)
                    if(w[i][k]&&w[k][j])
                        w[i][j]=1;
        if(vis)
        {
            for(int i=0; i<n; ++i)
                cout<<0;
            cout<<endl;
            continue;
        }
        for(int i=1; i<=n; ++i)
            for(int j=1; j<=n; ++j)
                if(w[i][j])
                {
                    if(w[j][i])
                        vis=1;
                    ++mi[i];
                    ++ma[j];
                }
        if(vis)
        {
            for(int i=0; i<n; ++i)
                cout<<0;
            cout<<endl;
            continue;
        }
        for(int i=1; i<=n; ++i)
        {
            if(mi[i]<=n/2&&ma[i]<=n/2)
                cout<<1;
            else
                cout<<0;
        }
        cout<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值