码迷,mamicode.com
首页 > 其他好文 > 详细

第十届山东省acm省赛补题(2)

时间:2019-05-16 00:02:27      阅读:31      评论:0      收藏:0      [点我收藏+]

标签:author   提醒   char   none   break   tin   lap   大小   shandong   

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4124

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

题意:一个序列有n个数,数值未知,给出m对大小关系,求对于每个数是否可以生成一个序列满足这个这个数为中位数,可以该位置输出1,反之输出0。

思路:考虑二元关系,可以建立有向图,考虑到数据范围很小,可以用floyed传递闭包,对于那些没有确定关系的数,他们的关系可以任意假设,所以只需要保证大(小)于他的元素个数少于n/2即可。感谢题目提供的特判提醒     另外进行特判时,必须在floyed传递闭包之后,因为这个wa了好几发。。。

代码如下:

技术图片
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int  maxn=105;
int a[maxn][maxn],ma[maxn],mi[maxn];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        memset(a,0,sizeof(a));
        memset(ma,0,sizeof(ma));
        memset(mi,0,sizeof(mi));
        int n,m,flag=0;
        scanf("%d %d",&n,&m);
        for (int i=1; i<=m; i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            if (u==v)  flag=1;
            a[u][v]=1;
        }
        for (int k=1; k<=n; k++)
        {
            for (int i=1; i<=n; i++)
            {
                for (int j=1; j<=n; j++)
                {
                    a[i][j]=a[i][j]||(a[i][k]&&a[k][j]);
                }
            }
        }
        for (int i=1; i<=n; i++)
        {
            for (int j=1; j<=n; j++)
            {
                if (a[i][j]&&a[j][i])
                {
                    flag=1;
                    break;
                }
            }
            if (flag) break;
        }
        if (flag)
        {
            for (int i=1; i<=n; i++)
                putchar(48);
            putchar(10);
            continue ;
        }
        for (int i=1; i<=n;i++)
        {
            for (int j=1; j<=n; j++)
            {
                if (a[i][j])
                    ma[i]++,mi[j]++;
            }
        }
        for (int i=1; i<=n; i++)
        {
            if (ma[i]<=n/2 && mi[i]<=n/2)
                putchar(49);
            else putchar(48);
        }
        putchar(10);
    }
    return 0;
}
View Code

 

第十届山东省acm省赛补题(2)

标签:author   提醒   char   none   break   tin   lap   大小   shandong   

原文地址:https://www.cnblogs.com/ztdf123/p/10872969.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!