2019山东ACM省赛L题题解(FLOYD传递闭包的变形)
本题地址
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传递闭包的变形)的更多相关文章
- 第八届山东ACM省赛F题-quadratic equation
这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Probl ...
- 福建工程学院第十四届ACM校赛M题题解 fwt进阶,手推三进制fwt
第九集,结束亦是开始 题意: 大致意思就是给你n个3进制的数字,让你计算有多少对数字的哈夫曼距离等于i(0<=i<=2^m) 思路: 这个是一个防ak题,做法是要手推公式的fwt 大概就这 ...
- 福建工程学院第十四届ACM校赛G题题解
外传:编剧说了不玩游戏不行 题意: 有n个石堆,我每次只能从某一堆中取偶数个石子,你取奇数个,我先手,先不能操作的人输.问最后谁能赢. 思路: 这个题仔细想想,就发现,取奇数的人有巨大的优势,因为假设 ...
- 福建工程学院第十四届ACM校赛B题题解
第二集,未来的我发量这么捉急的吗 题意: 有n个数,请问有多少对数字(i,j)(1<=i<j<=n),满足(a[i]^a[j])+((a[i]&a[j])<<1) ...
- 福建工程学院第十四届ACM校赛J题题解
第六集,想不到你这个浓眉大眼的都叛变革命了 题意: 给你两个只包含01的字符串S和T,问你在允许一次错误的情况下,T是否能成为S的子串 思路: 这个问题的解法挺多,我是用fft匹配的,也比较简单,针对 ...
- 2019长安大学ACM校赛网络同步赛 L XOR (规律,数位DP)
链接:https://ac.nowcoder.com/acm/contest/897/L 来源:牛客网 XOR 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- 牛客网 2018年东北农业大学春季校赛 L题 wyh的天鹅
链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒空间限制:C/C++ 262144K,其他语言524288 ...
- 2019浙江ACM省赛——部分题目
有一些题目过了我还没有重新写,先放一些我重新写好了的吧 签到题拿到了信心吧,9分钟写完两题,我们贼开心,我大哥说签到题有什么好开心的,如果不是我有一些地方卡了下,可能还是更快吧,还有就是测试案例多试了 ...
- 2019字节跳动冬令营day7娱乐赛19题题解
啊没去听讲题,也没发纸质题解,电子版题解也没有 为最后几个unsolve自闭了一段时间才全都A掉 3个队友写的我没看的题通过人数蛮多就不管了 题目地址:https://pan.baidu.com/s/ ...
随机推荐
- 进程管理与SELinux
进程(process): 将程序与进程的总结: 程序 (program):通常为 binary program ,放置在储存媒体中 (如硬盘.光盘.软盘.磁带等), 为实体文 件的型态存在 ...
- 第2节 storm实时看板案例:11、实时看板综合案例工程构建,redis的专业术语
redis当中的一些专业术语: redis缓存击穿 redis缓存雪崩 redis的缓存淘汰 =========================================== 详见代码
- rally问题合集
rally 执行过程中涉及到keystone的用例,需要调用adminurl,在-/rally/lib/python2.7/site-packages/rally/osclients.py(主机文件的 ...
- 将varchar2类型字段改成clob类型
--增加临时新字段alter table base_temp add temp clob; --将需要改成大字段的项内容copy到大字段中update base_temp set temp=con ...
- Caused by: java.net.ConnectException: Connection timed out: connect
发生这种情况的原因是:连接的路径发生错误
- Django 3.0 中连接mysql 8.0,可以不使用pymysql ,升级Mysqlclient即可
python 中,连接mysql一般都推荐用pymysql ,而且在django中,网上的教程都是这么连接mysql的. import pymysql pymysql.install_as_MySQL ...
- instance与可变参数合用,多态性
public class Doubt { public static void main(String[] args) { Dog d1=new Dog(); Dog d2=new Zangao(); ...
- loadrunner-11安装+破解+汉化
一.loadrunner-11安装下载地址:链接:https://pan.baidu.com/s/10meUz5DfkS8WleLSOalCtQ 提取码:iw0p 由于LR11安装包三个多G,没办法上 ...
- 1_01_MSSQL课程_基础入门
0. 课程安排: 课程共7天课,前两天SQL基础,后面三天Ado.Net ,最后两天数据库高级进阶学习. 1.数据库的概念 ->数据库就是数据仓库. ->DBMS:数据库管理系统.SQLS ...
- 牛客小白月赛3---G 旅游(树形dp)
题目链接:https://www.nowcoder.com/acm/contest/87/G 分析: 1.对于点cur,dp[cur][0]表示在该点住宿:dp[cur][1]表示其某个子结点住宿,自 ...