Блог пользователя I_love_PMK

Автор I_love_PMK, 12 лет назад, По-английски

http://www.spoj.com/problems/TPCPPLAR/

Given a directed graph G = (V,E), N vertices and M arcs. (N <= 150000, M <= 300000)

A node X called "acessable" to Y if there is a path from X to Y

A node X called "popular" if every node Y in V fulfill one of two conditions :

  1. X is acessable to Y

  2. Y is acessable to X

The Problem : Given graph G, count the number of popular node.

Someone has a solution for this problem.

I used Topo sorting, but I'm stucking at how to check all node previous i can go to i.

Sorry for my bad English.

  • Проголосовать: нравится
  • +10
  • Проголосовать: не нравится

»
12 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

edit: I've misread the problem statement

»
12 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Directed graph doesn't mean DAG.

»
12 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +9 Проголосовать: не нравится

Let's first topologically sort our DAG. Later in the text comparison A < B means that vertex A stands before vertex B in topological order.

Now let's notice that for vertex V an edge that connects vertices V1 < V and V2 > V doesn't influence on the fact whether V is popular. Let's remove all such edges and see whether there exists a vertex V1 < V that has zero output degree or V2 > V that has zero input degree, if so — V is not popular, otherwise it is.

To get an or O(E) solution, you need to iterate over the vertices in topological order and maintain all the in-out degrees.

»
12 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

We can transform this problem to this problem.