Sunday, 23 November 2014

UVa 10765 - Doves and bombs Solution

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 10010

struct Z{
    int pv, nd;
    bool operator < (const Z &p) const{
    if(pv==p.pv) return nd<p.nd;
    return pv>p.pv;
    }
};

vector<int>adj[MX];

int t, dis[MX], low[MX], parent[MX], child[MX];
bool isArti[MX], vis[MX];

void findAP(int u, int root)
{
    low[u] = dis[u] = ++t;
    vis[u]=true;
    int children=0;
    for(int i=0;i<adj[u].size();i++)
    {
        int v=adj[u][i];
        if(!vis[v])
        {
            children++;
            parent[v]=u;
            findAP(v, root);
            low[u]=min(low[u], low[v]);

            if(u==root && children>1)   child[u]=max(child[u], children-1), isArti[u]=true;     //special check for root
            else if(u!=root && low[v]>=dis[u]) child[u]++, isArti[u]=true;


        }
        else if(parent[u]!=v)
            low[u]=min(low[u], dis[v]);
    }
return;
}

vector<Z>v;
Z z;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n, m, x, y;
    while(cin>>n>>m && n)
    {
        while(cin>>x>>y)
        {
            if(x==-1) break;
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        int add=0;
        for(int i=0;i<n;i++)
            if(!vis[i]) add++, findAP(i, i);

        for(int i=0;i<n;i++)
        {
            z.nd=i;
            z.pv=child[i]+add;

            v.push_back(z);
        }

        sort(v.begin(), v.end());

        for(int i=0;i<m;i++)
            cout<<v[i].nd<<" "<<v[i].pv<<"\n";
        cout<<"\n";

        CLR(vis);
        CLR(isArti);
        CLR(child);
        v.clear();
        for(int i=0;i<n;i++) adj[i].clear();
    }
return 0;
}

2 comments: