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/ ...
随机推荐
- kd-tree理论以及在PCL 中的代码的实现(转载)
该文转自:https://www.cnblogs.com/li-yao7758258/p/6437440.html kd-tree理论以及在PCL 中的代码的实现 (小技巧记录:博客园编辑的网页界 ...
- 搞懂G1垃圾收集器
一.G1 GC术语Overview 1.1 并发 并发的意思是Java应用执行和垃圾收集活动可以同时进行 1.2 并行 并行的意思是垃圾收集运算是多线程执行的,比如CMS垃圾收集器的年轻代就是并行的, ...
- IdentityServer4专题之四:Authorization Endpoint、Token Endpoint、scope、Access Token和Refresh Token、授权服务器发生错误
1.Authorization Endpoint 它是与用户交互的端点,用户在此进行为客户端应用授权的操作,即authorization grant 2.Token Endpoint 端点,就是一个w ...
- 转,docker学习笔记
一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 -- Docke ...
- NLP之gensim
一. 利用 jieba 进行分词,关键词提取 利用gensim下面的corpora,models,similarities 进行语料库建立,模型tfidf算法,稀疏矩阵相似度分析 # -*- codi ...
- 记一次海洋cms任意代码执行漏洞拿shell(url一句话)
实验环境:海洋CMS6.54(后续版本已该洞已补) 1.后台登录尝试 这个站点是个测试站,站里没什么数据. 进入admin.php,是带验证码的后台登录系统,没有验证码的可以用bp爆破.有验证码的也有 ...
- ES 模糊查询
参考:https://blog.csdn.net/u011262847/article/details/78007119
- 序列化sys随笔补充
sys是在和python解释器做交互 sys.path----模块查找的顺序 sys.path.append() sys.path.insert()sys.argv---只能在终端执行 sys.arg ...
- OpenJ_Bailian - 1088 滑雪(记忆化搜索)
题意:给定一个二维数组,一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小,输出可以滑行的最长区域的长度. 分析:对于每一个点,进行记忆化搜索.若某点可以向四周某几个点滑行,记忆化搜索求出 ...
- 008.Delphi插件之QPlugins,服务的两种调用方法
这个QPlugins自带的DEMO,大概的意思就是,创建2个服务类,程序启动的时候注册这2个服务类.点击不同的按钮,使用不同的方法来调用这个服务. 效果界面如下 unit Frm_Main; inte ...