求助!80pts
查看原帖
求助!80pts
982208
qizhihang楼主2024/10/12 19:44

我用了DFS

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cstdio>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <cmath>
#include <float.h>
#include <cfloat>
#include <string.h>
#include <cstring>
#include <string>
#include <ctype.h>
#include <cctype>
#include <time.h>
#include <ctime>
#include <limits.h>
#include <climits>
#include <limits>
#include <algorithm>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <iterator>
#include <set>
#include <list>
#include <deque>
#include <bitset>
#include <numeric>
using namespace std;

//常量
const int maxn = 1e5 + 5;

//结构体
struct edge
{
	int u , v , next;
}e[maxn];

//变量
int cnt , n , m , q , sure , cntmt;
int head[maxn] , rec[maxn][maxn] , xfkmt[maxn] , yfkmt[maxn];
bool vis[maxn][maxn];

//STL容器

//函数
void adde(int x,int y)
{
	e[++cnt].u = x;
	e[cnt].v = y;
	e[cnt].next = head[x];
	head[x] = cnt;
}

void dfs(int now , int depth)
{
	if (sure == 1)
    {
        return ;
    }
	if (rec[now][depth] == -1 || vis[now][depth])
	{
        return ;
	}
	if (rec[now][depth] == 1)
    {
		sure = 1;
        return ;
	}
	if (depth == 1)
    {
		for (int i = head[now] ; i ; i = e[i].next)
		{
			if (e[i].v == 1)
            {
                sure = 1;
                break;
            }
		}
		if (sure != 1)
		{
            rec[now][depth] = -1;
		}
		return ;
	}
	for (int i = head[now] ; i ; i = e[i].next)
    {
		dfs(e[i].v , depth - 1);
		vis[e[i].v][depth - 1] = 1;
		cntmt ++;
		xfkmt[cntmt] = e[i].v;
		yfkmt[cntmt] = depth - 1;
	}
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr) , cout.tie(nullptr);
    cin >> n >> m >> q;
    int u , v;
    for(int i = 1 ; i <= m ; i ++)
    {
    	cin >> u >> v;
    	adde(u , v);
		adde(v , u);
    }
    int l;
    for(int k = 1 ; k <= q ; k ++)
    {
    	cin >> u >> l;
    	cntmt = 0 , sure = -1;
		dfs(u , l);
    	if (sure == 1)
    	{
            printf("Yes\n");
    	}
    	else
        {
            printf("No\n");
    	}
    	rec[u][l] = sure;
    	for (int i = 1 ; i <= cntmt ; i ++)
    	{
    		vis[xfkmt[i]][yfkmt[i]]=!vis[xfkmt[i]][yfkmt[i]];
    	}
    }
    return 0;
}

我用了DFS

2024/10/12 19:44
加载中...