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

dfs过,构造有向图时粗心wa了两发~~

Posted by Bang1314 at 2018-04-09 17:02:09 on Problem 1691
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;

#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define ll long long
#define lowbit(i) i&(-i)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int maxn=16;
const int mod=1e9+7;
const double eps=1e-6;

int n,ans;
bool vis[maxn];

typedef struct node{
    int x1,y1,x2,y2;
    int color;
}Node;

Node temp[maxn];

bool graph[maxn][maxn];

bool IsOk(int x){
    for(int i=0;i<n;i++){
        if(i==x) continue;
        if(graph[x][i]){
            if(vis[i]) continue;
            else return false;
        }
    }
    return true;
}
void dfs(int cur,int step,int col){
    if(cur==n){
        ans=min(ans,step);
        return ;
    }
    for(int i=0;i<n;i++){
        if(!vis[i]&&(temp[i].x1==0||IsOk(i))){
            vis[i]=true;
            if(temp[i].color==col) dfs(cur+1,step,temp[i].color);
            else dfs(cur+1,step+1,temp[i].color);
            vis[i]=false;
        }
    }
}
void build_graph(){
    memset(graph,false,sizeof(graph));
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            if(i==j) continue;
            if(temp[j].x2<=temp[i].x1&&((temp[j].y1>=temp[i].y1&&temp[j].y1<=temp[i].y2)||(temp[j].y1<=temp[i].y1&&temp[j].y2>=temp[i].y2)||(temp[j].y2>=temp[i].y1&&temp[j].y2<=temp[i].y2))) graph[i][j]=true;
        }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        ans=INF;
        memset(vis,false,sizeof(vis));
        for(int i=0;i<n;i++)
            scanf("%d%d%d%d%d",&temp[i].x1,&temp[i].y1,&temp[i].x2,&temp[i].y2,&temp[i].color);
        build_graph();
        dfs(0,0,0);
        cout<<ans<<endl;
    }
    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