Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

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

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

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
  • Проголосовать: не нравится

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

edit: I've misread the problem statement

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

Directed graph doesn't mean DAG.

»
10 лет назад, # |
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.

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

    I'm wondering this test case

    1 -> 2, 1 -> 3, 2 -> 4, 3 -> 4

    After topo sorting it become : 1 2 3 4

    In degree : 0 1 1 2

    Out degree : 2 1 1 0

    Let's consider V = 2, no V1 < V has zero out degree or V2 > V has zero in degree, but 2 isn't a popular vertice.

    • »
      »
      »
      10 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      You haven't removed the 1->3 edge.

      • »
        »
        »
        »
        10 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        oh thank you.

      • »
        »
        »
        »
        10 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        If with remove edge for each vertex V separately, a vertex can be removed many time. I think that in that case it will be TLE. If we remove an edge at most one.

        Example:

        1->2, 1-3, 2->4, 3->4 like I_love_PMK example.

        We remove 1->3 then 3 will have input degree equal to 0. After that, 1 will be not popular but actually, 1 is a popular vertex.

        Could you explain more about your idea ?

        • »
          »
          »
          »
          »
          10 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          It's pretty obvious that an edge (V1; V2) will be removed only for vertices V1 < V < V2. I said that we'll traverse the vertices in topological order, and this has some sense. For each edge (V1; V2) we can remove it after processing vertex V1 and add it before processing vertex V2.

          Well, we'll need to maintain the number of zero-out-degree vertices to the left of V and the number of zero-in-degree vertices to the right of V, but I think it shouldn't be too hard to understand how to do that.

        • »
          »
          »
          »
          »
          10 лет назад, # ^ |
            Проголосовать: нравится -25 Проголосовать: не нравится

          RIP a Hoà

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

We can transform this problem to this problem.