1
#include<bits/stdc++.h>
#define fr(i1,m) for(int i1=0;i1<m;i1++)
using namespace std;
int main()
{
long n,m;
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
queue<long>q;
vector<long>vc;
long x,y;
long d[2002]={0},in[10010]={0},u,i;
long g[200][200]={0};
fr(i,m)
{
cin>>x>>y;
g[x][y]=1;
in[y]++;
}
for(i=1;i<=n;i++)
{
if(in[i]==0)
{
q.push(i);
vc.push_back(i);
}
}
while(!q.empty())
{
u=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(g[u][i]==0)
continue;
in[i]--;
if(in[i]==0)
{
q.push(i);
vc.push_back(i);
}
}
}
for(i=0;i<vc.size();i++)
{
cout<<vc[i];
cout<<" ";
}
cout<<"\n";
}
return 0;
}
2.
int taken[55] = {};
int n, take[55][55], list[55] indegree[55];
int i, j, k;
// when take[a][b] = 1, that means a must come before b
// indegree[i] = number of items that that must come before i
// when taken[i] = 1, means we already have taken ith item
int invalid = 0;
for(i=0; i<n; i++) {
for(j=0; j<n; j++) if( !indegree[j] && !taken[j] ) {
taken[j] = 1;
list[i] = j;
// in this step we are taking item j
// we'd update the indegree[k] of items that depended on j
for(k=0; k<n; k++)
if( !taken[k] && take[j][k] ) --indegree[k];
break;
}
if( j == n ) {
invalid = 1;
break;
}
}
if( invalid ) printf("There is no solution\n");
else for(i=0; i<n; i++) printf("%d\n", list[i] );
#include<bits/stdc++.h>
#define fr(i1,m) for(int i1=0;i1<m;i1++)
using namespace std;
int main()
{
long n,m;
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
queue<long>q;
vector<long>vc;
long x,y;
long d[2002]={0},in[10010]={0},u,i;
long g[200][200]={0};
fr(i,m)
{
cin>>x>>y;
g[x][y]=1;
in[y]++;
}
for(i=1;i<=n;i++)
{
if(in[i]==0)
{
q.push(i);
vc.push_back(i);
}
}
while(!q.empty())
{
u=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(g[u][i]==0)
continue;
in[i]--;
if(in[i]==0)
{
q.push(i);
vc.push_back(i);
}
}
}
for(i=0;i<vc.size();i++)
{
cout<<vc[i];
cout<<" ";
}
cout<<"\n";
}
return 0;
}
2.
int taken[55] = {};
int n, take[55][55], list[55] indegree[55];
int i, j, k;
// when take[a][b] = 1, that means a must come before b
// indegree[i] = number of items that that must come before i
// when taken[i] = 1, means we already have taken ith item
int invalid = 0;
for(i=0; i<n; i++) {
for(j=0; j<n; j++) if( !indegree[j] && !taken[j] ) {
taken[j] = 1;
list[i] = j;
// in this step we are taking item j
// we'd update the indegree[k] of items that depended on j
for(k=0; k<n; k++)
if( !taken[k] && take[j][k] ) --indegree[k];
break;
}
if( j == n ) {
invalid = 1;
break;
}
}
if( invalid ) printf("There is no solution\n");
else for(i=0; i<n; i++) printf("%d\n", list[i] );
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন