• 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=105;
    typedef long long ll;
    ll s[maxn][maxn];
    int main()
    {
        ll n,m;    
            while(cin>>n>>m)
        {
            memset(s,0,sizeof(s));
            for(int i=1;i<=m;i++)
            {
                ll a,b;
                cin>>a>>b;
                s[a][b]=1;
            }
            for(int k=1;k<=n;k++)
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=n;j++)
                        {
                            if(s[i][k]&&s[k][j])
                                s[i][j]=1;
                        }
            ll ans=0;
            for(int i=1;i<=n;i++)
            {
                ll t=0;
                for(int j=1;j<=n;j++)
                {
                    if(s[i][j]||s[j][i])
                        t++;
                }
                if(t==n-1)
                    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=1005;
    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,0,sizeof(e));
            memset(in,0,sizeof(in));
            memset(out,0,sizeof(out));
            for(int i=1;i<=m;i++)
                {
                     ll x,y;
                     cin>>x>>y;
                     e[x][y]=1;
                }
            for(int k=1;k<=n;k++)
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=n;j++)
                        {
                            if(e[i][k]&&e[k][j])
                                e[i][j]=1;
                        }                //以上为传递闭包的基本操作
            ll ok1=0;
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    {
                        if(e[i][j]&&e[j][i])
                            {
                                ok1=1;break;
                            }
                    }                  //判断矛盾,如果出现i>j,j>i||i==j,直接就ok1=1;全0;

    // cout<<ok1<<endl; if(ok1) { for(int i=1;i<=n;i++) printf("0"); cout<<endl; } else { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(i!=j&&e[i][j]) { in[j]++; out[i]++; }        //核心代码。 }     for(int i=1;i<=n;i++) { if(in[i]>n/2||out[i]>n/2) { printf("0"); } else { printf("1"); } } cout<<endl; } } }

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

  • 相关阅读:
    ubuntu apt 命令相关
    linux 常用命令
    sqli-labs 通关详解(sql注入知识点整理)
    运维手册2---查看机子的系统信息
    运维手册1---ssh命令:使用密钥文件进行登陆
    Java中的==和equals,常量池
    Java Persistence
    VSCode常用插件汇总
    VSCode常用插件之Auto Rename Tag使用
    ESLint 使用指南
  • 原文地址:https://www.cnblogs.com/liyexin/p/10887059.html
Copyright © 2020-2023  润新知