Cod sursa(job #1941906)

Utilizator Corneliu10Dumitru Corneliu Corneliu10 Data 27 martie 2017 17:44:11
Problema Felinare Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <map>
#include <cstdio>
#include <algorithm>
#include <vector>
#define inf 1<<30
#define NMAX 10000

using namespace std;

struct grad
{
    int g, nod;
    vector<int> mc;
};
grad gi[NMAX],ge[NMAX];

map<int, int> M;
int sol[NMAX],nrmax;

int compare(grad a,grad b)
{
    return a.g < b.g;
}

void ver(grad gr,int type)
{
    int i,gas=0;
    for(i=0;i<gr.mc.size();i++)
        if(M.find(gr.mc[i]) != M.end())
        {
            if(M[gr.mc[i]]!=0)
            {
                gas=1;
            }
        }
    if(!gas)
    {
        for(i=0;i<gr.mc.size();i++)
            if(M.find(gr.mc[i]) != M.end())
            {
                M[gr.mc[i]]=1;
            }
        nrmax++;
        if(type == 1)
            sol[gr.nod]+=2;
        else
            sol[gr.nod]++;
    }
}

int main()
{
    int n,m,i,x,y;
    freopen("felinare.in","r",stdin);
    freopen("felinare.out","w",stdout);

    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++) gi[i].nod = ge[i].nod = i;
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        M[x*1000+y] = 0;
        gi[y].g++;
        gi[y].mc.push_back(x*1000+y);

        ge[x].g++;
        ge[x].mc.push_back(x*1000+y);
    }

    sort(gi+1,gi+n+1,compare);
    sort(ge+1,ge+n+1,compare);
    gi[n+1].g = ge[n+1].g = inf;

    int pi=1,pe=1;
    while(pi!=n+1 || pe!=n+1)
    {
        if(ge[pe].g < gi[pi].g && pe<=n)
        {
            ver(ge[pe],2);
            pe++;
        }
        else if(pi <=n)
        {
            ver(gi[pi],1);
            pi++;
        }
    }

    printf("%d\n",nrmax);
    for(i=1;i<=n;i++)
        printf("%d\n",sol[i]);
}