zoukankan      html  css  js  c++  java
  • ZOJ 3605 Find the Marble


    Find the Marble

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts, Alice puts the pots in one line and puts the marble in one of the pots. After that, Bob cannot see the inside of the pots. Then Alice makes a sequence of swappings and Bob guesses which pot the marble is in. In each of the swapping, Alice chooses two different pots and swaps their positions.

    Unfortunately, Alice's actions are very fast, so Bob can only catch k of m swappings and regard these k swappings as all actions Alice has performed. Now given the initial pot the marble is in, and the sequence of swappings, you are asked to calculate which pot Bob most possibly guesses. You can assume that Bob missed any of the swappings with equal possibility.

    Input

    There are several test cases in the input file. The first line of the input file contains an integer N (N ≈ 100), then N cases follow.

    The first line of each test case contains 4 integers nmk and s(0 < s ≤ n ≤ 50, 0 ≤ k ≤ m ≤ 50), which are the number of pots, the number of swappings Alice makes, the number of swappings Bob catches and index of the initial pot the marble is in. Pots are indexed from 1 to n. Then m lines follow, each of which contains two integers ai and bi (1 ≤ aibi ≤ n), telling the two pots Alice swaps in the i-th swapping.

    Outout

    For each test case, output the pot that Bob most possibly guesses. If there is a tie, output the smallest one.

    Sample Input

    33 1 1 11 23 1 0 11 23 3 2 22 33 21 2

    Sample Output

    213
    Author: GUAN, Yao
    Contest: The 9th Zhejiang Provincial Collegiate Programming Contest



    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    long long int dp[55][55][55],a[55],b[55];

    int main()
    {
        int T;
        cin>>T;
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        int n,m,k,s;
        cin>>n>>m>>k>>s;
        for(int i=1;i<=m;i++)
            cin>>a>>b;
        dp[0][0][s]=1;
        for(int i=1;i<=m;i++)
        {
            dp[0][s]=1;
            for(int j=1;j<=i&&j<=k;j++)
            {
                dp[j][a]=dp[i-1][j-1][b];
                dp[j][b]=dp[i-1][j-1][a];

                for(int t=1;t<=n;t++)
                {
                    dp[j][t]+=dp[i-1][j][t];
                    if(t!=a&&t!=b)
                        dp[j][t]+=dp[i-1][j-1][t];
                }
            }
        }
        int ans=1;
        for(int i=2;i<=n;i++)
        {
            if(dp
    [k][ans]<dp
    [k])
                ans=i;
        }
        cout<<ans<<endl;
    }
        return 0;
    }

  • 相关阅读:
    Pathfinding 模板题 /// BFS oj21413
    poj2186-Popular Cows(强连通分支)
    求有向强连通分量 模板
    F. Relatively Prime Powers (求([2,n],内不是次方的数量)
    【2018沈阳赛区网络预选赛J题】Fantastic Graph 【有上下界的网络流】
    Zoj 2314 Reactor Cooling(无源汇有上下界可行流)
    有上下界网络流模板
    有上下界网络流建模方法
    有上下界的网络流问题 的理解分析
    ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven(约束第K短路)
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350931.html
Copyright © 2011-2022 走看看