Pagini recente » Cod sursa (job #1246153) | Cod sursa (job #2380945) | Cod sursa (job #552192) | Cod sursa (job #736413) | Cod sursa (job #831133)
Cod sursa(job #831133)
#include <fstream>
#include <vector>
#include <algorithm>
#define MAX_N ((const unsigned int)8192)
typedef std::vector<int> Neighbours;
class Node
{
public:
int in;
int out;
bool bFelIn;
bool bFelOut;
public:
Node() :
in(0), out(0), bFelIn(true), bFelOut(true)
{
}
};
Node Graf[MAX_N];
class Edge
{
public:
int x;
int y;
float costx;
float costy;
public:
Edge(int _x, int _y) :
x(_x), y(_y), costx(0), costy(0)
{
}
float getMinCost() const
{
if (costx < costy)
return costx;
else
return costy;
}
bool operator < (const Edge &_other) const
{
return this->getMinCost() < _other.getMinCost();
}
};
class EdgesComparator
{
public:
bool operator()(const Edge &_edge1, const Edge &_edge2) const
{
return _edge1 < _edge2;
}
};
std::vector<Edge> Edges;
int main()
{
std::ifstream fin("felinare.in");
std::ofstream fout("felinare.out");
int N, M, x, y;
int sum = 0;
fin>>N>>M;
sum = N * 2;
for (int i = 0; i < M; ++ i)
{
fin>>x>>y;
Edges.push_back(Edge(x, y));
Graf[x].out ++;
Graf[y].in ++;
}
for (int i = 0; i < M; ++ i)
{
Edges[i].costx = 1. / Graf[Edges[i].x].out;
Edges[i].costy = 1. / Graf[Edges[i].y].in;
}
std::sort(Edges.begin(), Edges.end(), EdgesComparator());
for (int i = 0; i < M ; ++ i)
{
if (Edges[i].costx < Edges[i].costy)
{
if (Graf[Edges[i].x].bFelOut)
{
sum --;
Graf[Edges[i].x].bFelOut = false;
}
}
else
{
if (Graf[Edges[i].y].bFelIn)
{
sum --;
Graf[Edges[i].x].bFelIn = false;
}
}
}
fout<<sum<<'\n';
for (int i = 0; i < N; ++ i)
{
if (Graf[i].bFelIn && Graf[i].bFelOut)
fout<<3<<'\n';
else if (Graf[i].bFelIn)
fout<<2<<'\n';
else if (Graf[i].bFelOut)
fout<<1<<'\n';
else
fout<<0<<'\n';
}
fin.close();
fout.close();
return 0;
}