Pagini recente » Monitorul de evaluare | Cod sursa (job #342067) | Cod sursa (job #1675358) | Cod sursa (job #1016439) | Cod sursa (job #1840708)
#include <fstream>
using namespace std;
ifstream fin ("plan.in");
ofstream fout ("plan.out");
struct nod
{
int inf;
nod *urm;
};
nod *p[300], *u[300], *x[300], *y[300];
int n, m, nr, s[300], pred[300];
void cit()
{
int i, j, k;
fin >> n >> m;
nod *q;
for (k=1; k<=m; k++)
{
fin >> i >> j;
if (p[i] == 0)
{
q = new nod;
q->inf = j;
q->urm = 0;
p[i] = q;
u[i] = q;
}
else
{
q = new nod;
q->inf = j;
q->urm = 0;
u[i]->urm = q;
u[i] = q;
}
if (x[j] == 0)
{
q = new nod;
q->inf = i;
q->urm = 0;
x[j] = q;
y[j] = q;
}
else
{
q = new nod;
q->inf = i;
q->urm = 0;
y[j]->urm = q;
y[j] = q;
}
}
}
void adancimes(int k)
{
nod *q;
s[k] = nr;
q = new nod;
q = p[k];
while (q != 0)
{
if (s[q->inf] == 0)
adancimes(q->inf);
q = q->urm;
}
}
void adancimep(int k)
{
nod *q;
pred[k] = nr;
q = new nod;
q = x[k];
while (q != 0)
{
if (pred[q->inf] == 0)
adancimep(q->inf);
q = q->urm;
}
}
int main()
{
cit();
int i, j;
for (i=1; i<=n; i++)
{
if (s[i] == 0)
{
nr++;
adancimes(i);
adancimep(i);
for (j=1; j<=n; j++)
if (s[j] == 0 || pred[j] == 0)
{
s[j] = 0;
pred[j] = 0;
}
}
}
fout << nr-1 << '\n';
return 0;
}