@atcoder - ARC092F@ Two Faced Edges


@description@

Given a directed graph for each side do once asked:

After the flip side of this, strongly connected components of the original image any impact?

The number of points N ≤ 1000, the number of edges M ≤ 200000.

The original title Portal.

@solution@

See 5s time, daring guess time complexity O (NM).

An edge u-> v reversing the impact:
if this edge in the strongly connected components (equivalent to the presence of the path to v u), when the other path does not exist from u to v, the flip strongly connected reduced.
If not, when there is another path from u to v, the flip increased strongly connected.

That is for an edge u-> v, we need sentenced to two things: whether there is a path from v to u; if there is another path from u to v.

Before a violent O (NM) can do, and do not need to also write a strongly connected.
As for the latter. What we consider another path means: There is a way to u as a starting point and without u, the first edge than u-> v the path.

FIG full then the search for each point u, if this path is determined for each point x of the first side is only one.
This can be found just how, because of the status of each point will be updated twice (only one, more than one).

@accepted code@

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 1000;
const int MAXM = 200000;

vector<int>G[MAXN + 5]; int to[MAXM + 5];
void addedge(int u, int i) {G[u].push_back(i);}

int tag[MAXN + 5], que[2*MAXN + 5];
bool ans1[MAXM + 5], ans2[MAXM + 5];

int n, m;
int main() {
    scanf("%d%d", &n, &m);
    for(int i=1;i<=m;i++) {
        int a; scanf("%d%d", &a, &to[i]);
        addedge(a, i);
    }
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) tag[j] = 0;
        int s = 1, t = 0;
        for(int p=0;p<G[i].size();p++)
            tag[que[++t] = to[G[i][p]]] = G[i][p];
        while( s <= t ) {
            int x = que[s++], y = tag[x];
            for(int p=0;p<G[x].size();p++) {
                int q = to[G[x][p]];
                if( q != i ) {
                    if( tag[q] != -1 ) {
                        if( tag[q] == 0 )
                            tag[que[++t] = q] = y;
                        else if( tag[q] != y )
                            tag[que[++t] = q] = -1;
                    }
                }
                else ans1[G[x][p]] = 1;
            }
        }
        for(int p=0;p<G[i].size();p++)
            if( tag[to[G[i][p]]] == -1 ) ans2[G[i][p]] = 1;
    }
    for(int i=1;i<=m;i++)
        puts(ans1[i] ^ ans2[i] ? "diff" : "same");
}

@details@

Like this dense graph, the star with the vector storage memory faster (and more friendly cache) compared to the previous chain. In particular, this constant problem of great impact.

As for the cache is Gesha. . . According to my THUWC2020 entry learned, probably in addition to memory, cpu it has a more recent temporary space called cache.
If you have access to the same element, then to cache it is very friendly, and therefore will be faster.

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/12468758.html