本题地址

https://cn.vjudge.net/contest/302014#problem/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
这个是除了那五道“签到题“以外较为简单的一道算法题,当然比赛时没做,还是太菜了,就A了三道,说多了都是泪啊。

下面是我看了题解以后自己的理解:

  就是给你n个数,m个关系,表示a比b大,最后要你在满足这些关系的条件下,判断每一位数有没有可能是中位数,如果可能这一位输出1,否则输出0。

  注意矛盾的情况直接输出n个0,矛盾情况两种,输入a=b,或者存在两个数u,v,u>v并且v>u,(这句是别人的,不好意思)

  此为floyd的传递闭包。对于此算法有一个基础题:http://poj.org/problem?id=3660

附上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
typedef long long ll;
ll s[maxn][maxn];
int main()
{
ll n,m;
while(cin>>n>>m)
{
memset(s,,sizeof(s));
for(int i=;i<=m;i++)
{
ll a,b;
cin>>a>>b;
s[a][b]=;
}
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(s[i][k]&&s[k][j])
s[i][j]=;
}
ll ans=;
for(int i=;i<=n;i++)
{
ll t=;
for(int j=;j<=n;j++)
{
if(s[i][j]||s[j][i])
t++;
}
if(t==n-)
ans++;
}
cout<<ans<<endl;
}
}

而此题为传递闭包的变形。

    基本的传递闭包如上面的代码所示。在这道题中,需要加入两个入度和出度的数组,根据我的理解,in[j]这个数组应该是记录比j大的数的数目了,out[i]是记录比i小的数的数目了。

比如样例1,我打出了以下数据:

        1  2  3  4  5

in     0  2  0  3  3 

out   3  2  3  0  0

  根据样例我们可以推知:1>2,  3>2,  2>4  ,2>5。

  比较明了的关系有:  1>2>4,  3>2>5。

  1:比1大的未知,所以in[1]=0。比1小的可以确定的有2,4,5,所以out[1]=3;

  2:比2大有4,5,所以in[2]=2, 比2小的有1,3,所以out[2]=2;

  3:   比3大的未知,所以in[3]=0,比3小的有2,5,4,所以out[3]=3;

  。。。。。。。。以此类推,可得上表。对于一个数,要想是中位数,那么比它大的数或者比它小的数都不能大于(n/2),出现这种情况,肯定输出0。

  该样例有5个数,5/2=2,所以结果为01000

  贴上代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=;
typedef long long ll;
ll e[maxn][maxn],in[maxn],out[maxn];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,m;
cin>>n>>m;
memset(e,,sizeof(e));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for(int i=;i<=m;i++)
{
ll x,y;
cin>>x>>y;
e[x][y]=;
}
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(e[i][k]&&e[k][j])
e[i][j]=;
}                //以上为传递闭包的基本操作
ll ok1=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(e[i][j]&&e[j][i])
{
ok1=;break;
}
}                  //判断矛盾,如果出现i>j,j>i||i==j,直接就ok1=1;全0; // cout<<ok1<<endl;
if(ok1)
{
for(int i=;i<=n;i++)
printf("");
cout<<endl;
}
else
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(i!=j&&e[i][j])
{
in[j]++;
out[i]++;
}        //核心代码。
}    
for(int i=;i<=n;i++)
{
if(in[i]>n/||out[i]>n/)
{
printf("");
}
else
{
printf("");
}
}
cout<<endl;
}
}
}

自己掌握的知识太少,学了floyd后不懂得拓展。只有出去一趟才知道自己有多菜。在此与各位共勉,加油!

2019山东ACM省赛L题题解(FLOYD传递闭包的变形)的更多相关文章

  1. 第八届山东ACM省赛F题-quadratic equation

    这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Probl ...

  2. 福建工程学院第十四届ACM校赛M题题解 fwt进阶,手推三进制fwt

    第九集,结束亦是开始 题意: 大致意思就是给你n个3进制的数字,让你计算有多少对数字的哈夫曼距离等于i(0<=i<=2^m) 思路: 这个是一个防ak题,做法是要手推公式的fwt 大概就这 ...

  3. 福建工程学院第十四届ACM校赛G题题解

    外传:编剧说了不玩游戏不行 题意: 有n个石堆,我每次只能从某一堆中取偶数个石子,你取奇数个,我先手,先不能操作的人输.问最后谁能赢. 思路: 这个题仔细想想,就发现,取奇数的人有巨大的优势,因为假设 ...

  4. 福建工程学院第十四届ACM校赛B题题解

    第二集,未来的我发量这么捉急的吗 题意: 有n个数,请问有多少对数字(i,j)(1<=i<j<=n),满足(a[i]^a[j])+((a[i]&a[j])<<1) ...

  5. 福建工程学院第十四届ACM校赛J题题解

    第六集,想不到你这个浓眉大眼的都叛变革命了 题意: 给你两个只包含01的字符串S和T,问你在允许一次错误的情况下,T是否能成为S的子串 思路: 这个问题的解法挺多,我是用fft匹配的,也比较简单,针对 ...

  6. 2019长安大学ACM校赛网络同步赛 L XOR (规律,数位DP)

    链接:https://ac.nowcoder.com/acm/contest/897/L 来源:牛客网 XOR 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  7. 牛客网 2018年东北农业大学春季校赛 L题 wyh的天鹅

    链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒空间限制:C/C++ 262144K,其他语言524288 ...

  8. 2019浙江ACM省赛——部分题目

    有一些题目过了我还没有重新写,先放一些我重新写好了的吧 签到题拿到了信心吧,9分钟写完两题,我们贼开心,我大哥说签到题有什么好开心的,如果不是我有一些地方卡了下,可能还是更快吧,还有就是测试案例多试了 ...

  9. 2019字节跳动冬令营day7娱乐赛19题题解

    啊没去听讲题,也没发纸质题解,电子版题解也没有 为最后几个unsolve自闭了一段时间才全都A掉 3个队友写的我没看的题通过人数蛮多就不管了 题目地址:https://pan.baidu.com/s/ ...

随机推荐

  1. Linux下,Tomcat启动成功,发现ip:8080访问失败

    Linux下,Tomcat启动成功,发现ip:8080访问失败 Chasel_H 2018.04.23 20:47* 字数 195 阅读 566评论 0喜欢 3 相信很多人都和我一样,在Linux环境 ...

  2. shell脚本中执行sql脚本(mysql为例)

    1.sql脚本(t.sql) insert into test.t value ("LH",88); 2.shell脚本(a.sh     为方便说明,a.sh与t.sql在同一目 ...

  3. 在webView中除去广告

    首先建一个ADFilterTool.java类 代码如下 import android.content.Context; import android.content.res.Resources; p ...

  4. MariaDB——备份与恢复

    备份和恢复 为什么要备份?   灾难恢复:硬件故障.软件故障.自然灾害.黑客攻击.误操作   测试   要注意的点:   备份需要多少时间   能够容忍多少的数据丢失   恢复数据需要在多长时间完成  ...

  5. Shiro登录身份认证(从SecurityUtils.getSubject().login(token))到Realm的doGetAuthenticationInfo

    ssm框架下,controller接收到登录请求交给Service并开始处理流程: 1.Service的login方法: @Service public class SysUserServiceImp ...

  6. Dom4j 使用简介(全而好的文章)

    版权声明:本文由冰云完成,首发于CSDN,未经许可,不得使用于任何商业用途.文中代码部分引用自DOM4J文档.欢迎转载,但请保持文章及版权声明完整.如需联络请发邮件:icecloud(AT)sina. ...

  7. SciPy k均值聚类

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  8. Unbutu下装oracle

    Ubuntu 16.04安装Oracle 11gR2入门教程图文详解 转自         https://www.linuxidc.com/Linux/2017-12/149797.htm  原文作 ...

  9. stm32CubeMx CAN 发送数据

    平台  STM32F429 软件  STM32CubeMx 5.0.0 固件库  STM32Cube_FW_F4_V1.23.0 目的: 实现 CAN 的发送 一  简介 CAN是控制器局域网络(Co ...

  10. Java开发程序员必须要学会的linux命令总结

    查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件. find . -name "*.xml" 递归查找所有的xml文 ...