Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求教,询问原因。。。

Posted by mickeyandkaka at 2011-11-10 21:19:22 on Problem 2446
想法是二分匹配 把图当作国际象棋的棋盘 黑色的点在X 白色的在Y 这样 黑白最多512个 边最多512×4   可是这样写程序不是TLE就是WA  改的很大后才AC
想知道自己哪里想错了

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#define clr(a,b)    memset(a,b,sizeof(a))
using namespace std;

const int XMAX = 2000;//这两行小于2000好像就不行
const int YMAX = 2000;
const int EDGE_MAX = 4100;

struct Edge
{
    int ed,nxt;
}edge[EDGE_MAX];

int num;
int head[XMAX];
int vis[XMAX],mat[YMAX];

bool dfs(int x)
{
    vis[x] = 1;
    for(int i=head[x]; ~i; i=edge[i].nxt)
    {
        int y = edge[i].ed;
        if(vis[mat[y]] == 1)    continue;
        if(mat[y] == 0 || dfs(mat[y]))
        {
            mat[y] = x;
            return true;
        }
    }
    return false;
}

int hungary(int n)
{
    int ans = 0;
    clr(mat,0);
    for(int i=1; i<=n; i++)
    {
        clr(vis,0);
        if(dfs(i))  ans++;
    }
    return ans;
}

void addedge(int st,int ed)
{
    edge[++num].ed = ed;
    edge[num].nxt = head[st];
    head[st] = num;
}

int n,m,k;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool gra[35][35];
int id[35][35];

int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        clr(gra,false);clr(head,-1);num = 0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                gra[i][j] = true;

        int black = 0,white = 0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                if(((i&1)^(j&1))==0)
                    id[i][j] = ++black;
                else
                    id[i][j] = ++white;
            }

        for(int i=1; i<=k; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            gra[b][a] = false;
        }

        if((m*n-k)%2 == 1)  {printf("NO\n");continue;}
        
        int node = 0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                if(((i&1)^(j&1))==0 && gra[i][j])
                {
                    for(int p=0; p<4; p++)
                    {
                        if(gra[i+dx[p]][j+dy[p]])
                        {
                            int s=id[i][j],t=id[i+dx[p]][j+dy[p]];
                            addedge(s,t);
                            node++;
                        }
                    }
                }
            }

            if(hungary(node)*2 + k == m*n)
                printf("YES\n");
            else
                printf("NO\n");
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator