TopCoder SRM628 DIV2

250

根据棋盘染色的性质判断即可

// BEGIN CUT HERE

// END CUT HERE
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <utility>
#include <iterator>

using namespace std;

typedef long long ll;

#define clr(x,a) memset(x,a,sizeof(x))
#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<(n);++i)
#define FOR(i,l,h) for(i=(l);i<=(h);++i)
#define FORD(i,h,l) for(i=(h);i>=(l);--i)


class BishopMove
{
        public:
        int howManyMoves(int r1, int c1, int r2, int c2)
        {
           if(r1==r2 && c1==c2) return 0;
           if(fabs(r1-r2)==fabs(c1-c2)) return 1;
           if((r1+c1)%2==(r2+c2)%2) return 2;
           return -1;
        }

// BEGIN CUT HERE
	
// END CUT HERE

};
// BEGIN CUT HERE

// END CUT HERE


500

直接匹配就行了

// BEGIN CUT HERE

// END CUT HERE
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <utility>
#include <iterator>

using namespace std;

typedef long long ll;

#define clr(x,a) memset(x,a,sizeof(x))
#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<(n);++i)
#define FOR(i,l,h) for(i=(l);i<=(h);++i)
#define FORD(i,h,l) for(i=(h);i>=(l);--i)

char backet[7]={'(', ')', '[', ']', '{', '}'};

class BracketExpressions
{
        public:
        int id[20];
        int sel[20];
        int n,nx;
        string str;
        bool jud(string ss){
            char s[90];
            int p=0;
            int i,j,k;
            REP(i,n){
                if(ss[i]=='('||ss[i]=='{'||ss[i]=='[')
                    s[p++]=ss[i];
                else{
                    if(ss[i]==')'&&(p<=0||s[p-1]!='('))
                        return false;
                    if(ss[i]==']'&&(p<=0||s[p-1]!='['))
                        return false;
                    if(ss[i]=='}'&&(p<=0||s[p-1]!='{'))
                        return false;
                    p--;
                }
            }
            if(p==0)
                return true;
            return false;
        }
        bool dfs(int x){
            int i,j,k;
            if(x==nx){
                string tmp=str;
                REP(i,nx)
                    tmp[id[i]]=backet[sel[i]];

                return jud(tmp);
            }
            else{
                REP(i,6){
                    sel[x]=i;
                    if(dfs(x+1)) return true;
                }
            }
            return false;
        }
        string ifPossible(string expression)
        {
            int i,j,k;
            n=expression.length();
            str=expression;
            nx=0;
            REP(i,n)
                if(str[i]=='X') id[nx++]=i;
            bool ok=dfs(0);

            if(ok) return "possible";
            return "impossible";
        }

// BEGIN CUT HERE
	
// END CUT HERE

};
// BEGIN CUT HERE

// END CUT HERE



1000

强连通缩点(可用floyd) + DP

// BEGIN CUT HERE

// END CUT HERE
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <ctime>
#include <utility>
#include <iterator>

using namespace std;

typedef long long ll;

#define clr(x,a) memset(x,a,sizeof(x))
#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<(n);++i)
#define FOR(i,l,h) for(i=(l);i<=(h);++i)
#define FORD(i,h,l) for(i=(h);i>=(l);--i)

#define MAXN 100
#define MAXM 10000

struct node
{
	int to,next;
};

node edge[MAXM];
int head[MAXN],en,dag[MAXN],en2,n;


void add(int a,int b)
{
	edge[en].to=b;
	edge[en].next=head[a];
	head[a]=en++;
}

int low[MAXN],dfn[MAXN];
int stack[MAXN],top,set[MAXN],col,num;
bool vis[MAXN],instack[MAXN];

void tarjan(int u)
{
	vis[u]=1;
	dfn[u]=low[u]=++num;
	instack[u]=true;
	stack[++top]=u;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!vis[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else
			if(instack[v])
				low[u]=min(dfn[v],low[u]);
	}
	if (dfn[u]==low[u])
	{
		int j;
		col++;
		do
		{

			j=stack[top--];
			instack[j]=false;
			set[j]=col;
		}
		while (j!=u);
	}
}

void solve()
{
	int i;
	top=col=num=0;
	memset(instack,0,sizeof(instack));
	memset(vis,0,sizeof(vis));
	memset(set,-1,sizeof(set));
	for (i=0;i<n;i++)
		if (!vis[i])
			tarjan(i);
}

class InvariantSets
{
        public:
        int G[60][60];
        ll dp[60];
        bool rt[60];

        ll dfs(int f){
            int i,j,k;
            if(dp[f]!=-1) return dp[f];
            ll res=1;
            ll mul=1;
            REP(i,n)
                if(G[f][i]) mul*=dfs(i);
            return dp[f]=mul+res;
        }

        long long countSets(vector <int> f)
        {
            int i,j,k;
            memset(head,-1,sizeof(head));en=0;
            n=sz(f);
            REP(i,n)
                add(f[i],i);
            solve();
            clr(G,0);clr(rt,0);
            REP(i,n){
                if(set[i]==set[f[i]]) continue;
                G[set[f[i]]][set[i]]=1;
                rt[set[i]]=1;
            }
            clr(dp,-1);
            ll res=1;
            FOR(i,1,col){
                if(rt[i]) continue;
                res*=dfs(i);
            }
            return res;
        }

// BEGIN CUT HERE
	
// END CUT HERE

};
// BEGIN CUT HERE

// END CUT HERE



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值