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

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;
}



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

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

时间: 2024-07-28 22:16:34

第十届山东省acm省赛补题(2)的相关文章

第十届山东省ACM省赛题解

点击跳转 A - Calandar H - Stones in the Bucket L - Median 原文地址:https://www.cnblogs.com/CSGOBESTGAMEEVER/p/10891007.html

第七届山东省ACM省赛

激动人心的省赛终于结束了…平静下来再回头看真的感觉一波三折…先是赛前毫无预兆的查出突发性耳聋…伴随而来的就是左耳听力下降.轻微耳鸣.极个别情况下的头晕…不过这都还好,毕竟药物可以恢复…热身赛只过了一道输出济南有多少泉水的水题,竟然第二次就猜对了,有个队交了四五十次…山师很心机的把酒店安排在了商业区.闹市和女子学院附近…(开个玩笑)为了不败第二天人品,我老老实实地待在了酒店,并没有出去嗨…正式赛开了…比赛打得多了,真的不紧张了…或许是喝了磊神指定饮料-红牛的作用…A是个签到题,我和Julyc讨论一

第二届山东省ACM省赛回顾 Crack Mathmen(打表)

Crack Mathmen 题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2165 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Since mathmen take security very seriously, they communicate in encrypted messages. They cipher

【第六届山东省ACM竞赛】B题 Lowest Unique Price(SDUT3252)

题目链接:Here 这一题是我今年省赛最大的遗憾啊.诶...想想就觉得伤心啊.这一题其实不难,但是比赛时,我已经先到了怎么做,但是由于鄙人的失误,结果导致我们队后两个小时的时间都耗在那里了.越想越觉得可惜啊.我们现在看看这题的思路吧. 这一题,貌似大多人都是有STL的set做的.其实,这一题可以用线段树做(不知道线段树的童鞋请移步:这里),而且还是简单的单点更新问题.不知道单点更新的同学请移步:这里.虽然我做线段树的题目不是很多,但是我发现了一个规律.利用这个规律就可以更好的判断,此题是否为线段

sdut 2603 Rescue The Princess(算是解析几何吧)(山东省第四届ACM省赛A题)

题目地址:sdut 2603 Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess

Sdut 2416 Fruit Ninja II(山东省第三届ACM省赛 J 题)(解析几何)

Time Limit: 5000MS Memory limit: 65536K 题目描写叙述 Haveyou ever played a popular game named "Fruit Ninja"? Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for NvidiaTegra 2 based Android devices) is a video game developed by Hal

2017年山东省ACM省赛总结

----但求努力到问心无愧 这次比赛我们是作为友谊队去的,本来我们队选拔赛成绩并不是很好,是去不了的,但伟大的教主大人牛逼地又要到了几个省赛友谊队的名额,才让我们有这次见识大场面比赛的机会,在这里我们先要感谢教主,还有就是感谢陪同的老师们,还有一直忙里忙外的负责人学长和同学们. 然后就是检讨我们自己了.这次比赛我们真的打的很不好,虽然比赛方有好多地方弄得有点欠缺.首先是热身赛,开始我们以为会有好多题,发下题目来看原来只有3个,好有三个题就三个题,那就做,但是我们还没开始看题,就意识到一个问题:这

[水]浙大校赛补题

本来以为会巨难无比又有别的比赛可以打于是赛后补题,结果发现似乎略水,反正是找到了四个水题先贴下来 ZOJ-4099 J #include<iostream> #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; int t; ll x, y, n; int main() { cin >> t; while (t--) { scanf("%lld

第六届山东ACM省赛总结

省赛总结 省赛打完了,结果在意料之中,教主的判断还是很准的,我们的水平在银牌区中段,运气好可以进金牌区.感觉队伍打的还不错,感谢给力的队友,但感觉我个人还是打的有点乱. 开场我先简单整体的翻了一下习题册,依旧abc一人一题,我开b,李睿易a王成瑞c,b读读感觉是个模拟,尽管感觉不难,但是之前每逢模拟必卡题,就先跳过了,和王成瑞去看c,是个博弈,有点小自信,因为省赛前一段时间做过几道博弈,又看到有一队5min C题1Y,自信的推c了,第一次直接对3取余wa一发,李睿易看出a水,喊王成瑞去写a,我继