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

C++ WA G++ AC 求解释

Posted by root123 at 2012-11-05 23:02:02 on Problem 2942
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
#define maxn 1010
#define maxm 1000009
bool map[maxn][maxn];
int min(int a, int b) {return a < b ? a : b;}

struct E
{
    int v, next;
}edge[maxm<<3];

int head[maxn], tot;
int n, m;
void add(int s, int t)
{
    edge[tot].v = t;
    edge[tot].next = head[s];
    head[s] = tot++;

    edge[tot].v = s;
    edge[tot].next = head[t];
    head[t] = tot++;
}

int stack[maxn], top;
int dfn[maxn], low[maxn];
int num, id;
vector<int> ans[maxn];
void dfs(int u, int fa)
{
    int i;
    low[u] = dfn[u] = ++id;
    stack[++top] = u;
    for(i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if(v == fa) continue; 
        if(!dfn[v])
        {
            dfs(v, u);
            low[u] = min(low[u], low[v]);
            if(dfn[u] <= low[v])
            {
                ans[num].push_back(u);
                while(1)
                {
                    int w = stack[top--];
                    ans[num].push_back(w);
                    if(w == v) break;
                }
                if(ans[num].size()<=2) ans[num].clear();
                else num++;
            }
        }
        else low[u] = min(low[u], dfn[v]);
    }
}

int col[maxn];
bool color(int u, int fa, int k)
{
    int i;
    for(i = 0; i < ans[k].size(); i++)
    {
        int y = ans[k][i];
        if(fa == i || !map[ans[k][u]][y]) continue;
        if(col[i] == -1) 
        {
            col[i] = 1^col[u];
            if( color(i, u, k) ) return 1;
        }
        else if(col[i] == col[u]) return 1;
    }
    return 0;
}

bool vis[maxn];
void solve()
{
    num = 0; top = 0;
    memset(dfn, 0, sizeof(int)*(n+1));
    int i;
    for(i = 1; i <= n; i++)
        if(!dfn[i]) { id = 0; dfs(i, -1); }
    memset(vis, 0, sizeof(int)*(n+1));
    for(i = 0; i < num; i++)
    {
        memset(col, -1, sizeof(int)*(n+1));
        col[0] = 1;
        if(color(0, -1, i))
        {
            for(int x = 0; x < ans[i].size(); x++)
                vis[ans[i][x]] = 1;
        }
    }    
    int cnt = 0;
    for(i = 1; i <= n; i++)
        if(!vis[i]) cnt++;
    printf("%d\n", cnt);
}
int main()
{
    int i, j;
    while( ~scanf("%d%d", &n, &m) && n || m)
    {
        for(i = 0; i <= n; i++)
			for(j = 0; j <= n; j++)
				map[i][j] = 1;
        int x, y;
        for(i = 0; i <= n; i++)
        {
            ans[i].clear();
            map[i][i] = 0;
        }
        while(m--)
        {
            scanf("%d%d", &x, &y);
            map[x][y] = map[y][x] = 0;
        }
        memset(head, -1, sizeof(int)*(n+1));
        tot = 0;
        for(i = 1; i <= n; i++)
            for(j = i+1; j <= n; j++)
                if(map[i][j]) add(i, j);
        solve();
    }
    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