zoom 8.10 笔试

zoom 8.10 笔试

第一题就是建树 dfs一边

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace  std;
using ll= long long;
using pii =pair<int,int> ;

const int N=1e5+10;

int n;
char str[N];
int h[N],e[N<<1],ne[N<<1],idx;
bool st[N];

void add(int x,int y){
    e[idx]=y,ne[idx]=h[x],h[x]=idx++;
}

struct Node{
    int id,x,y;
};

ll bfs(){
    ll res=0;
    queue<Node> q;
    q.push({1,0,0});
    while(q.size()){
        auto cur=q.front();q.pop();
        if(st[cur.id]) continue;
        if(str[cur.id]=='R') cur.x++;
        else cur.y++;
        res+=abs(cur.x-cur.y);
        st[cur.id]=true;
        for(int i=h[cur.id];~i;i=ne[i]){
            int v=e[i];
            if(st[v]) continue;
            q.push({v,cur.x,cur.y});
        }
    }
    return res;
}


int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>str[i];
    memset(h,-1,sizeof h);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        add(x,y),add(y,x);
    }

    cout<<bfs()<<endl;
    return 0;
}












第二题 并查集 记录下连通块大小即可

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace  std;
using ll= long long;
using pii =pair<int,int> ;

const int N=1e5+10;

int n,m;

unordered_map<string,int> name;
unordered_map<string ,vector<int>> h;

int f[N],si[N];
int idx=0;

int get(int x){
    if(x!=f[x]) f[x]=get(f[x]);
    return f[x];
}

void merge(int x,int y){
    int fx=get(x),fy=get(y);
    if(fx!=fy) {
        f[fx]=fy;
        si[fy]+=si[fx];
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>m;

    for(int i=1;i<N;i++) {
        f[i]=i;
        si[i]=1;
    }

    while(m--){
        int op;
        cin>>op;
        string s;
        if(op==1){
            cin>>s>>n;
            for(int i=0;i<n;i++){
                string t;
                cin>>t;
                if(name.count(t)) h[s].push_back(name[t]);
                else {
                    name[t]=++idx;
                    h[s].push_back(name[t]);
                }
            }
            for(int i=0;i<h[s].size()-1;i++){
                merge(h[s][i],h[s][i+1]);
            }
        }else if(op==2) {
            cin>>s;
            if(!h.count(s)) cout<<"error"<<endl;
            else{
                int fx=get(h[s][0]);
                cout<<si[fx]-h[s].size()<<endl;
            }
        }
    }
    return 0;
}












#Zoom##ZOOM笔试##做完zoom2023秋招笔试,人麻了#
全部评论
我就喜欢这种考数据结构的题,那种贪心回溯啥的总是想不出
2
送花
回复
分享
发布于 2022-08-10 20:35
大佬牛逼
1
送花
回复
分享
发布于 2022-08-10 20:33
秋招专场
校招火热招聘中
官网直投
大佬 第二题的思路能说的详细点吗
1
送花
回复
分享
发布于 2022-08-10 20:33
第一题卡96%,第二题知道是并查集,一时间忘了如何下手0%,寄!😃
1
送花
回复
分享
发布于 2022-08-10 20:34
牛逼
1
送花
回复
分享
发布于 2022-08-10 20:48
第二题看成了要返回推荐股票的名称,然后就不会做了😂
1
送花
回复
分享
发布于 2022-08-10 21:48
牛啊
点赞
送花
回复
分享
发布于 2022-08-10 20:34
楼主就是牛
点赞
送花
回复
分享
发布于 2022-08-10 20:39
不是 不能用c++嘛😓
点赞
送花
回复
分享
发布于 2022-08-10 20:42
牛逼
点赞
送花
回复
分享
发布于 2022-08-10 20:45
第二题用的不带权重的并查集,后面超时想加权来不及了😭
点赞
送花
回复
分享
发布于 2022-08-10 20:46
请问大伙,这代码为什么会超内存呢?只过了46%
点赞
送花
回复
分享
发布于 2022-08-10 20:48
真牛啊 自己是嘎了
点赞
送花
回复
分享
发布于 2022-08-10 20:53
铁铁,考虑考虑咱网易呗,三大事业群都在向你招手哦,速来速来 https://www.nowcoder.com/discuss/1009725
点赞
送花
回复
分享
发布于 2022-08-10 21:48
你好,可以看一下我主页讨论帖。亿联网络,厂商,通信行业独角兽,薪资福利行业领先,有兴趣的话可以直接去我讨论帖内推链接,hr直通车https://neitui.italent.cn/yealink/sharejobs?shareId=5e36baaf-1cf5-47cd-8973-6294f8c3ef68在帖子下留言(姓名+岗位方便查进度哈)
点赞
送花
回复
分享
发布于 2022-08-13 19:46

相关推荐

13 29 评论
分享
牛客网
牛客企业服务