2021-08-22

字节笔试(后端)

字节笔试(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<<" ";
   }
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值