1 / 2
Sep 2023
#include <bits/stdc++.h>
using namespace std;
#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)
#define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++)
#define FORL(a, b, c) for (int(a) = (b); (a) <= (c); (a)++)
#define FORR(a, b, c) for (int(a) = (b); (a) >= (c); (a)--)
#define INF INT_MAX
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define f first
#define s second
#define pb push_back
#define POB pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
/*
.______        ___      .______      .______        ______   .___________..___________.  ______        _______.
|   _  \      /   \     |   _  \     |   _  \      /  __  \  |           ||           | /  __  \      /       |
|  |_)  |    /  ^  \    |  |_)  |    |  |_)  |    |  |  |  | `---|  |----``---|  |----`|  |  |  |    |   (----`
|   ___/    /  /_\  \   |      /     |      /     |  |  |  |     |  |         |  |     |  |  |  |     \   \
|  |       /  _____  \  |  |\  \----.|  |\  \----.|  `--'  |     |  |         |  |     |  `--'  | .----)   |
| _|      /__/     \__\ | _| `._____|| _| `._____| \______/      |__|         |__|      \______/  |_______/
*/
vector<vector<int>> a;
vector<int> check;
vector<int> level;
bool flag;
int n, r, m;

void bfs(int dinh, int count_max)
{
    level.assign(n + 1, 0);

    queue<int> q;
    q.push(dinh);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        cout << u << " ";

        if (level[u] > count_max)
            continue;

        if (check[u] != 0 and check[u] != dinh)
        {
            flag = false;
            return;
        }
        check[u] = dinh;

        for (int i = 0; i < a[u].size(); i++)
        {
            int v = a[u][i];
            if (check[v] == false)
            {
                q.push(v);
                level[v] = level[u] + 1;
            }
        }
    }
}
void start()
{
    cin >> n >> r >> m;
    a.clear();
    a.resize(n + 1);
    check.assign(n + 1, 0);
    level.assign(n + 1, 0);
    flag = true;
    FOR(i, r)
    {
        int temp1, temp2;
        cin >> temp1 >> temp2;
        a[temp1].pb(temp2);
        a[temp2].pb(temp1);
    }
    FOR(i, m)
    {
        int temp1, temp2;
        cin >> temp1 >> temp2;
        bfs(temp1, temp2);
        // cout << "**";
        if (flag == false)
            break;
        FORL(i, 1, n)
        cout << check[i] << " ";
        cout << endl;
    }
    if (flag == true)
        cout << "Yes";
    else
        cout << "No";
    cout << endl;
}

int main()
{
#ifndef ONLINE_JUDGE
    // freopen("", "r", stdin);
    // freopen("", "w", stdout);
#endif

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--)
    {
        start();
    }
    return 0;
}
  • created

    Sep '23
  • last reply

    Sep '23
  • 1

    reply

  • 401

    views

  • 2

    users

  • 1

    link

I gave you program the sample input, and instead of

No
Yes

it gave

1 2 3 1 1 1
2 No
2 1 4 4 3 2 2 0 0
3 4 2 2 3 0
Yes

Did you forget to take out the debug output?

AKBAR5