Submission #4103831


Source Code Expand

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>
#include<cassert>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

inline ll read(){
    ll Hashimoto=0;
	bool Kanna=1;
    char I_Love=getchar();
    while(I_Love<'0'||I_Love>'9'){
        if(I_Love=='-')Kanna=0;
        I_Love=getchar();
    }
    while(I_Love>='0'&&I_Love<='9'){
        Hashimoto=Hashimoto*10+I_Love-'0';
        I_Love=getchar();
    }
    return (Kanna?Hashimoto:-Hashimoto);
}
template<typename T1,typename T2> inline void Umax(T1 &a,T2 b){
	if(a<b)a=b;
}
template<typename T1,typename T2> inline void Umin(T1 &a,T2 b){
	if(a>b)a=b;
}

#define I_Love_Hashimoto_Kanna main
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define filein(s) freopen(s,"r",stdin);
#define fileout(s) freopen(s,"w",stdout);
#define file freopen("I_Love_Hashimoto_Kanna.out","w",stdout);
#define RE cout<<"I_Love_Hashimoto_Kanna"<<endl;
#define Tone(Kanna) cout<<Kanna<<endl;
#define Tall(Hashimoto,Kanna) for(int I_Love=0;I_Love<(Kanna);++I_Love)cout<<Hashimoto[I_Love]<<(I_Love==(Kanna)-1?"\n":" ");
#define FOR(I_Love,Hashimoto,Kanna) for(int I_Love=Hashimoto;I_Love<(Kanna);++I_Love)
#define MP(Hashimoto,Kanna) make_pair(Hashimoto,Kanna)
#define REV(Kanna) reverse(Kanna.begin(),Kanna.end());
#define SORT(Kanna) sort(Kanna.begin(),Kanna.end());
#define UNIQUE(Kanna) Kanna.erase(unique(Kanna.begin(),Kanna.end()),Kanna.end());
#define inf (1000000000)
#define linf (1000000000000000000ll)
#define mod (1000000007)

int n,m;
int indegree[111111];
vi g[111111];
queue<int>q;
bool v[111111];
int ans[111111];

int I_Love_Hashimoto_Kanna() {
	//完全想清楚了再开始写。
	//写不顺、不知道怎么写、很乱的时候,停下来好好想想。
	//做得慢总比做不出好。
	fastio;
	n=read();m=read();
	FOR(i,1,n+m){
		int a=read()-1,b=read()-1;
		g[a].push_back(b);
		indegree[b]++;
	}
	int root=-1;
	FOR(i,0,n){
		if(indegree[i]==0)root=i;
	}
	assert(root!=-1);
	ans[root]=-1;
	v[root]=1;
	q.push(root);
	while(!q.empty()){
		int x=q.front();
		q.pop();
		FOR(i,0,g[x].size()){
			int to=g[x][i];
			ans[to]=x;
			indegree[to]--;
			if(indegree[to]!=0)continue;
			q.push(to);
			v[to]=1;
		}
	}
	FOR(i,0,n){
		printf("%d\n",ans[i]+1);
	}
	
	return 0;
}

Submission Info

Submission Time
Task D - Restore the Tree
User FelixChen
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2796 Byte
Status AC
Exec Time 33 ms
Memory 7424 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 34
Set Name Test Cases
Sample a01, a02
All a01, a02, b03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34
Case Name Status Exec Time Memory
a01 AC 2 ms 2944 KiB
a02 AC 2 ms 2944 KiB
b03 AC 2 ms 2944 KiB
b04 AC 2 ms 2944 KiB
b05 AC 2 ms 2944 KiB
b06 AC 33 ms 7424 KiB
b07 AC 33 ms 7424 KiB
b08 AC 33 ms 7424 KiB
b09 AC 9 ms 3456 KiB
b10 AC 9 ms 3456 KiB
b11 AC 9 ms 3456 KiB
b12 AC 24 ms 5112 KiB
b13 AC 23 ms 5112 KiB
b14 AC 24 ms 5112 KiB
b15 AC 29 ms 6144 KiB
b16 AC 23 ms 4736 KiB
b17 AC 28 ms 5760 KiB
b18 AC 19 ms 4096 KiB
b19 AC 32 ms 6656 KiB
b20 AC 21 ms 4480 KiB
b21 AC 28 ms 5760 KiB
b22 AC 23 ms 4736 KiB
b23 AC 24 ms 5116 KiB
b24 AC 25 ms 5248 KiB
b25 AC 27 ms 5504 KiB
b26 AC 32 ms 6784 KiB
b27 AC 21 ms 4352 KiB
b28 AC 26 ms 5376 KiB
b29 AC 33 ms 6784 KiB
b30 AC 32 ms 6656 KiB
b31 AC 31 ms 6272 KiB
b32 AC 29 ms 6144 KiB
b33 AC 29 ms 6016 KiB
b34 AC 23 ms 4480 KiB