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/ ...
随机推荐
- ionic3记录之APP运行时网络判断
判断设备网路是否正常: 安装插件: ionic cordova plugin add cordova-plugin-network-information npm install --save@nat ...
- Android 获取当前日期距离过期时间的日期差值的完整方法直接使用
/*** * 获取当前日期距离过期时间的日期差值 * @param endTime * @return */public String dateDiff(String endTime) { Strin ...
- mutiset的简单介绍转载
原文链接:https://blog.csdn.net/sodacoco/article/details/84798621 c++语言中,multiset是<set>库中一个非 ...
- Day9 - A - Apple Catching POJ - 2385
Description 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速移动到另外一棵APP树下接APP(移动时间可 ...
- Day3-M-Cable master POJ1064
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...
- C# 篇基础知识5——委托和事件
事件处理程序是基于“委托”机制运行的. 1.委托 (1)委托的定义和使用 有时需要将一个函数作为另一个函数的参数,这时就要用到委托(Delegate)机制.例如设计一个马戏表演函数: //定义委托 d ...
- php:数据库封装类
<?phpclass DBDA{ public $host="localhost"; public $uid="root"; publi ...
- Day10:关于桃子的和关于游戏新的设想(顺手做个记录孩子吃喝拉撒的工具)
公历2015年6月3日~ 北京时间晚上8:42~ 在美中宜和~ 一个叫桃子的小美女出生了! 没错!小桃子终于出生了!真心不容易啊! 6月3日 03:00 AM 老婆推我,叫我起来,她说她肚子疼,还想上 ...
- hibernate部分源码解析and解决工作上关于hibernate的一个问题例子(包含oracle中新建表为何列名全转为大写且通过hibernate取数时如何不用再次遍历将列名(key)值转为小写)
最近在研究系统启动时将数据加载到内存非常耗时,想着是否有办法优化!经过日志打印测试发现查询时间(查询时间:将数据库数据查询到系统中并转为List<Map>或List<*.Class& ...
- C++Review15_内存管理
一.野指针 定义指针变量时最好初始化为NULL: 内存回收后,指针也用完了,这时候也需要及时将指针置为NULL: 指针就像野狗一样,为了防止它乱指,除了在使用期间,别的时候都需要置为NULL.这样它就 ...