Cattle-off practice match 14 E - the shortest distance undirected graph (bfs + bitset)

A link: https://ac.nowcoder.com/acm/contest/82/E
Source: Cattle-off network

Undirected shortest distance in FIG.
Time limit: C / C ++ 2 seconds and 4 seconds languages other
space restrictions: C / C ++ 262144K, other languages 524288K
64bit the IO the Format: LLD%

Title Description

A n points undirected graph, m queries, each query give some (X I , Y I )

Order dist (x, y) represents the shortest distance between points x and y in the figure, dist (x, x) = 0, if x, y is not connected dist (x, y) = inf

Each query graph how many points v and at least one of the given query (X I , Y I ) satisfies dist (v, X I Yi) <=

Enter a description:

The first three rows represent the number of n, m, q

After two m row per row x, y represents a bordered between x and y, is a right side

Q times after inquiry, asking each give you a number a

After the number of row 2a, 2i-1 of number X I and 2i number Y I represents a tuple (X I , Y I

Output Description:

Q output lines, each line represents a number of answers to this inquiry
Example 1

Entry

copy
5 6 6
2 3 
1 3 
2 5 
1 3 
3 2 
2 5
1
3 1
1
1 1
1
1 4
1
5 2
1
1 4
2
1 0 5 1

Export

copy
3
2
4
3
4
3

Remarks:

To 100% of the data, n-<= 1000, m <= 100000, q <= 
100000
A and <= 2,100,000

question is intended:
to give you a directed graph, then the q interrogation, each asking you num logarithmic x, y.
You ask how many nodes and the number of this num distance of at least one pair of x is less than or equal to y in FIG.

Ideas:
firstly bfs for each node to find the shortest it to other nodes.
An array DIS [i] [j] to maintain a distance from i to j,
then we theorem the bitset <1011> BS [1011] [1011]
BS [i] [j] [K] represents the i-th node distance or less there are no j k, there are only two states, namely 0/1
so use bitset perfectly maintained.
For treatment,
we post every node bfs, update bs

repd(j,1,n)
{
  bs[i][dis[i][j]][j]=1;
}

Also, because less so bs [i] [j] [ k] = 1 , then bs [i] [j ~ n ] [k] are 1 
so that we can be from 1 to n, with the bitset oR | = j to update the greater the distance must contain a smaller case.

Then the inquiry,
we need to make a go of all bitset 0 | = num that a bs [x] [y] can be.
The resulting bitset number 1 is the number of answers.

See details Code:
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int dis[maxn][maxn];
bitset<maxn> bs[maxn][maxn];
int m;
int q;
bitset<maxn> res;
std::vector<int> G[maxn];
void bfs(int x)
{
    dis[x][x] = 0;
    queue<int> q;
    q.push(x);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (auto y : G[u])
        {
            if (~dis[x][y])continue;
            dis[x][y] = dis[x][u] + 1;
            q.push(y);
        }
    }
}
int main()
{
    // freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    // freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    memset(dis, -1, sizeof(dis));
    gbtb;
    cin >> n >> m >> q;
    int x, y;
    repd(t, 1, m)
    {
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    repd(i, 1, n)
    {
        bfs(i);
        repd(j, 1, n)
        {
            bs[i][dis[i][j]][j] = 1;
        }
        repd(j, 1, n)
        {
            bs[i][j] |= bs[i][j - 1];
        }
    }
    int num;
    repd(t, 1, q)
    {
        cin >> num;
        res.reset();
        repd(i, 1, num)
        {
            cin >> x >> y;
            res |= bs[x][y];
        }
        cout << res.count() << endl;
    }



    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}
 
  

 

 

Guess you like

Origin www.cnblogs.com/qieqiemin/p/10961225.html