70pts|基于DFS的链式前向星做法
查看原帖
70pts|基于DFS的链式前向星做法
429818
Smithespics楼主2023/4/1 21:19

阿巴巴巴,一开始思路错了,想成了链式前向星

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct Node
{
    int to,next,p;
}node[10005*2];
int visit[10005*2];
int head[10005*2];
int n,m,t,a,b;
int cnt = 0;

void add(int u,int v)
{
    node[cnt].to   = v;
    node[cnt].next = head[u];
    head[u]        = cnt++;
}

void dfs(int x,int y)
{
    visit[x] = 1;
    node[x].p += y;
    for(int i = head[x];~i;i = node[i].next)
    {
        int to = node[i].to;
        if(!visit[to])
            dfs(to,y);
    }
}

signed main()
{
    cin >> n >> m;
    memset(head,-1,sizeof head);
    while(m--)
    {
        cin >> t >> a >> b;
        if(t == 1)
        {
            if(a == b)  continue;
            add(a,b);
            add(b,a);
        }
        else if(t == 2)
        {
            memset(visit,0,sizeof visit);
            dfs(a,b);
        }
    }
    for(int i = 1;i <= n;i++)
            cout << node[i].p << ' ';

    return 0;
}
2023/4/1 21:19
加载中...