本题地址

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. input文件类型上传,或者作为参数拼接的时候注意的问题!

    1.ajax请求参数如果为文本类型,直接拼接即可.如果为file类型就需要先获取文件信息 2.获取文件信息: HTML代码: <div class="form-group"& ...

  2. Iptables与LVS——从入门到放弃

    防火墙什么是防火墙?防火墙其实就是一个隔离的工具,工作于主机或者网络的边缘,对于进出本主机或者网络的报文根据事先定义好的网络规则做匹配监测.防火墙可以简单地划分为两大类:主机防火墙 网络防火墙     ...

  3. 小程序canvas 变换

    var ctx = wx.createCanvasContext('base'); var centerX = 375/ 2; var centerY = 200; var rotate = 90; ...

  4. hdu 1533 Going Home 最小费用最大流 (模板题)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. 分享一款免费的工控组态软件(PCHMI)

    PCHMI严格的讲它并不是一款组态软件,也不是一款SCADA软件,而是一个基于.NET构架的DLL文件,开发者可以使用微软的Visual Studio将PCHMI.DLL加载到工具箱里面进行二次开发. ...

  6. py交易

    下载之后发现是pyc文件,反编译一下 网站:https://tool.lu/pyc/ 发现其实就是base64转换为16进制,然后减去16再和32异或,就可以得到答案了 nctf{d3c0mpil1n ...

  7. iOS大V博客

    王巍的博客:王巍目前在日本横滨任职于LINE.工作内容主要进行Unity3D开发,8小时之外经常进行iOS/Mac开发.他的陈列柜中已有多款应用,其中番茄工作法工具非常棒. http://onevca ...

  8. @Autowired 和 @ Resource 的区别

    转 都是用来装配Bean的注解.都可以写在字段上,或写在setter方法上. @Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以 ...

  9. android名词

    NDK:Native Development Kit JNI:Java Native Interface

  10. 7.11 如何应用Varnish

    动态数据缓存 Step 1 修改devault.vcl文件 # This ) # man page for details on VCL syntax and semantics. # # Defau ...