#include <bits/stdc++.h>
using namespace std;

int main() {
    int N,M,st=-1,stn,nd=-1,ndn,a,b,max,num;
    cin>>N>>M;
    vector<int> A(N),C(N,0);
    vector<vector<int>> B(N,vector<int>());
    for(int i=0;i<M;i++){
        cin>>a>>b;
        C[a-1]++;
        B[a-1].push_back(b-1);
    }
    for(int i=0;i<N;i++){
        if(C[i]>nd){
            if(C[i]>st){
                nd=st;
                ndn=stn;
                st=C[i];
                stn=i;
            }else{
                nd=C[i];
                ndn=i;
            }
        }
    }
    for(int i=0;i<N;i++){
        A=C;
        A[i]=0;
        for(int j=0;j<B[i].size();j++){
            A[B[i][j]]++;
        }
        max=-1; num=-1;
        for(int j=0;j<B[i].size();j++){
            if(A[B[i][j]]>max){
                max=A[B[i][j]];
                num=B[i][j];
            }
        }
        if(A[stn]>max){
            max=A[stn];
            num=stn;
        }
        if(A[ndn]>max){
            max=A[ndn];
            num=ndn;
        }
        cout<<num<<endl;
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: