Pagini recente » Cod sursa (job #1331043) | Cod sursa (job #307639) | Cod sursa (job #2607989) | Cod sursa (job #2053487) | Cod sursa (job #165719)
Cod sursa(job #165719)
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAXM 100500
#define MAXN 50500
using namespace std;
struct s1
{
int x,y;
};
s1 b[MAXM],a[MAXM],p[MAXN],r[MAXN];
int m,n,t[MAXN],vS[MAXN],S=0;
bool cmp(s1 a, s1 b)
{
if (a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
void citire(FILE *f)
{
char s[50]; int x,y,j,i;
fscanf(f,"%d%d",&n,&m);
fgets(s,50,f);
for (i=0; i<m; i++)
{
memset(s,0,sizeof(s));
fgets(s,50,f); x=0; y=0; j=0;
while (s[j]>='0' && s[j]<='9')
{
x=x*10+s[j]-'0';
j++;
}
j++;
while (s[j]>='0' && s[j]<='9')
{
y=y*10+s[j]-'0';
j++;
}
b[i].x=x; b[i].y=y; t[y]=1;
}
for (i=1; i<=n; i++)
if (t[i]==0)
{
vS[S++]=i;
}
sort(b,b+m,cmp);
a[0].x=b[0].x; a[0].y=b[0].y;
for (i=1,j=1; i<m; i++)
if (b[i].x!=b[i-1].x || b[i].y!=b[i-1].y)
{
a[j].x=b[i].x;
a[j].y=b[i].y;
j++;
}
m=j;
for (i=0,j=1; i<m;)
{
while (a[i].x==j && i<m)
i++;
p[j].y=i; j++;
while (a[i].x!=j && j<n)
{
p[j].x=p[j].y=i;
j++;
}
p[j].x=i;
}
p[j].y=i;
}
void namegoeshere(int pz, int k)
{
t[pz]=1;
r[pz].x+=k;
r[pz].y=pz;
int i;
for (i=p[pz].x; i<p[pz].y; i++)
namegoeshere(a[i].y,k+1);
}
void afisare(FILE *f)
{
int i;
sort(r+1,r+n+1,cmp);
for (i=1; i<n; i++)
fprintf(f,"%d ",r[i].y);
fprintf(f,"%d\n",r[n].y);
}
int main()
{
FILE *in = fopen("sortaret.in","r");
FILE *out = fopen("sortaret.out","w");
citire(in); memset(t,0,sizeof(t));
for (int i=0; i<S; i++)
namegoeshere(vS[i],0);
afisare(out);
fclose(in);
fclose(out);
return 0;
}