ZOJ 4124 2019 ACM山东省赛 L题

Median

Time Limit: 1 Second       Memory Limit: 65536 KB

Recall the definition of the median of  elements where  is odd: sort these elements and the median is the -th largest element.

In this problem, the exact value of each element is not given, but  relations between some pair of elements are given. The -th relation can be described as , which indicates that the -th element is strictly larger than the -th element.

For all , is it possible to assign values to each element so that all the relations are satisfied and the -th element is the median of the  elements?

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers  and  (), indicating the number of elements and the number of relations. It's guaranteed that  is odd.

For the following  lines, the -th line contains two integers  and , indicating that the -th element is strictly larger than the -th element. It guaranteed that for all  or .

It's guaranteed that the sum of  of all test cases will not exceed .

Output

For each test case output one line containing one string of length . If it is possible to assign values to each element so that all the relations are satisfied and the -th element is the median, the -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.


Author: WANG, Yucheng
Source: The 10th Shandong Provincial Collegiate Programming Contest

 

 

省赛时做完5道题之后就没有在做出来题了,真难受。

当时想用拓扑来找到比他大的有多少个和比他小的有多少个。可惜拓扑没写出来,真菜啊。

问了一下人家山理大一的新生,人家说用Floyd跑一下就出来了,emmm。。。。 太菜了。菜哭了。

 

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

int n;
int graph[105][105];
int v[2][105];
void print(){
    for(int i=0;i<n;i++)
        putchar('0');
    puts("");
}
int floyd(){
    for(int k=1;k<=n;k++) // floyd
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(graph[i][k] && graph[k][j])
                    graph[i][j] = 1;

    for(int i=1;i<=n;i++){ // 判环
        for(int j=1;j<=n;j++){
            if(graph[i][j] && graph[j][i])
                return -1;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)if(graph[i][j]){ // graph[i][j] 表示 i严格大于j
            v[0][i]++; // v[0][i] 记录比 i 小的有多少
            v[1][j]++; // v[0][j] 记录比 j 大的有多少
        }
    }
    return 0;
}
int main(){
    int t, m;
    int x, y;
    scanf("%d",&t);
    while(t--){
        memset(graph,0,sizeof(graph));
        memset(v,0,sizeof(v));
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++){
            scanf("%d%d",&x,&y);
            graph[x][y] = 1;
        }
        if(floyd()==-1){// 有环
            print();
            continue;
        }
        for(int i=1;i<=n;i++){
            if(v[0][i]<=n/2 && v[1][i]<=n/2)
                putchar('1');
            else
                putchar('0');
        }
        puts("");
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/kongbb/p/10859540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值