TopCoder SRM628 DIV2

250

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

// BEGIN CUT HERE

// END CUT HERE
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 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



你可能感兴趣的:(TopCoderSRM)