字节笔试(后端)
字节笔试(2021年8月22日)
AC了三题,有点难,前面一二题未记录。
第三题
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
struct node1{
int x;
bool operator < (const node1 a)const &{
return a.x<x;
}
};
priority_queue<node1>q;
int n,m,a[maxn],c[maxn];
struct node{
int v,pos;
}b[maxn];
vector<int>vv[maxn];
void bfs(){
int cnt=1;
while(!q.empty()){
int t=q.top().x;
b[t].pos=t;
b[t].v=cnt;
cnt++;
q.pop();
int len=vv[t].size();
for(int i=0;i<len;i++){
int tt=vv[t][i];
c[tt]--;
if(c[tt]==0){
q.push({tt});
}
}
//if(cnt>1000000)break;
}
}
bool cmp(struct node a,struct node b){
return a.v<b.v;
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int cnt;
cin>>cnt;
cin>>a[i];
for(int j=1;j<cnt;j++){
int xx;
cin>>xx;
vv[a[i]].push_back(xx);
c[xx]++;
}
sort(vv[a[i]].begin(),vv[a[i]].end());
}
for(int i=1;i<=n;i++){
if(c[i]==0){
q.push({i});
}
}
bfs();
sort(b+1,b+n+1,cmp);
for(int i=1;i<=n;i++){
cout<<b[i].pos<<" ";
}
}