BestCoder Round #25 A,B

挺好的一场比赛,完全被自己的智商给碾压了啊。。。都是泪啊。。

A,判断有向图中是否有环,数据很小简单粗暴的暴力算法可解啊。暴力枚举有关系两个点判断反向是否可以找到,如果可以就说明有环。。。

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

using namespace std;

const int maxn = 210;

int vis[maxn];
int mp[maxn][maxn];

vector<int> g[maxn];

int flag;

void dfs(int x, int y)
{
    if(x == y)
    {
        flag = 1;
        return;
    }
    if(vis[x]) return;
    vis[x] = 1;
    int n = g[x].size();
    for(int i = 0; i < n; i++) dfs(g[x][i], y);
}

int main()
{
    int n;
    int m;
    while(cin >>n>>m)
    {
        int x, y;
        flag = 0;
        memset(mp, 0, sizeof(mp));
        for(int i = 0; i <= n; i++) g[i].clear();
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d",&x, &y);
            mp[x][y] = 1;
            g[x].push_back(y);
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(!mp[i][j]) continue;
                memset(vis, 0, sizeof(vis));
                dfs(j, i);
                if(flag) break;
            }
            if(flag) break;
        }
        if(flag) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}

B,dp题目。我们一行一行的考虑。dp[i][j],表示前i行,都满足了每一行至少有一个宝石的条件,而只有j列满足了有宝石的条件的情况有多少种。枚举第i+1行放的宝石数k,这k个当中有t个是放在没有宝石的列上的,那么我们可以得到转移方程:

dp[i+1][j+t]+=dp[i][j]*c[m-j][t]*c[j][k-t],其中c[x][y],意为在x个不同元素中无序地选出y个元素的所有组合的个数。

题解里面解释的很清楚了啊。。不再瞎说了啊。

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007

using namespace std;

const int maxn = 210;

LL dp[maxn][maxn];
LL cnk[maxn][maxn];

void init()
{
    memset(cnk, 0, sizeof(cnk));
    cnk[0][0] = 1LL;
    for(int i = 1; i < 51; i++) cnk[i][0] = cnk[i][i] = 1LL;
    for(int i = 2; i < 51; i++)
        for(int j = 1; j < i; j++) cnk[i][j] = (cnk[i-1][j-1] + cnk[i-1][j])%mod;
}

int main()
{
    int n, m;
    init();
    while(~scanf("%d %d",&n, &m))
    {
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1LL;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j <= m; j++)
            {
                for(int k = 1; k <= m; k++)
                {
                    for(int t = m; t >= 0; t--)
                    {
                        if(m-j < 0 || k-t < 0) continue;
                        dp[i+1][j+t] += (dp[i][j]*cnk[m-j][t]%mod)*cnk[j][k-t]%mod;
                        dp[i+1][j+t] %= mod;
                    }
                }
            }
        }
        cout<<dp[n][m]%mod<<endl;
    }
}

				
时间: 2024-08-06 20:06:09

BestCoder Round #25 A,B的相关文章

hdu 5154 Harry and Magical Computer(BestCoder Round #25)

Harry and Magical Computer                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 472    Accepted Submission(s): 222 Problem Description In reward

BestCoder Round #25 1002 Harry And Magic Box [dp]

传送门 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 165    Accepted Submission(s): 64 Problem Description One day, Harry got a magical box. The box is made of n*m grids. Ther

[BestCoder Round #25 1003]Harry and Christmas tree

Harry and Christmas tree Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 70    Accepted Submission(s): 3 问题描述 圣诞节的夜晚,哈利得到一棵圣诞树.这棵树由n个节点组成,以1号节点为根.树上的每个节点有一些不同颜色的礼物,于是哈利开始统计树上的礼物.哈利每次统计一个礼物,会记录一

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

BestCoder Round #33(zhx&#39;s submissions-手速题,注意判断00和0的情况)

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1459    Accepted Submission(s): 232 问题描述 作为史上最强的刷子之一,zhx在各大oj上交了很多份代码,而且多数都AC了. 有一天,zhx想数一数他在n  个oj上一共交了多少份代码.他现在已经统计出在第i  个oj上

BestCoder Round #90 //div all 大混战 一题滚粗 阶梯博弈,树状数组,高斯消元

BestCoder Round #90 本次至少暴露出三个知识点爆炸.... A. zz题 按题意copy  Init函数 然后统计就ok B. 博弈 题  不懂  推了半天的SG.....  结果这个题.... C 数据结构题   我写了半个小时分块   然后发现     改的是颜色.... 我的天  炸炸炸 D. 没看懂题目要干啥.....  官方题解要搞死小圆

BestCoder Round #4 前两题 hdu 4931 4932

第一题太水了.. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int a[6]; 7 int main(){ 8 int cas; 9 scanf( "%d", &cas ); 10 while( cas-- ){ 11 for( int i = 0; i <

BestCoder Round #88

传送门:BestCoder Round #88 分析: A题统计字符串中连续字串全为q的个数,预处理以下或加个cnt就好了: 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <ctime> 5 #include <cmath> 6 #include <iostream> 7 #include <algorithm> 8

HDU 5651 xiaoxin juju needs help(BestCoder Round #77 (div.1)1001)

传送门 xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 861    Accepted Submission(s): 243 Problem Description As we all known, xiaoxin is a brilliant coder. He knew **palin