Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8443227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:05:01+00:00 2026-06-10T09:05:01+00:00

(This is derived from a recently completed programming contest) You are given G, a

  • 0

(This is derived from a recently completed programming contest)

You are given G, a connected graph with N nodes and N-1 edges.

(Notice that this implies G forms a tree.)

Each edge of G is directed. (not necessarily upward to any root)

For each vertex v of G it is possible to invert zero or more edges such that there is a directed path from every other vertex w to v. Let the minimum possible number of edge inversions to achieve this be f(v).

By what linear or loglinear algorithm can we determine the subset of vertexes that have the minimal overall f(v) (including the value of f(v) of those vertexes)?

For example consider the 4 vertex graph with these edges:

A<--B
C<--B
D<--B

The value of f(A) = 2, f(B) = 3, f(C) = 2 and f(D) = 2…

..so therefore the desired output is {A,C,D} and 2

(note we only need to calculate the f(v) of vertexes that have a minimal f(v) – not all of them)

Code:

For posterity here is the code of solution:

int main()
{
    struct Edge
    {
        bool fwd;
        int dest;
    };

    int n;
    cin >> n;

    vector<vector<Edge>> V(n+1);

    rep(i, n-1)
    {
        int src, dest;
        scanf("%d %d", &src, &dest);

        V[src].push_back(Edge{true, dest});
        V[dest].push_back(Edge{false, src});
    }

    vector<int> F(n+1, -1);

    vector<bool> done(n+1, false);

    vector<int> todo;
    todo.push_back(1);
    done[1] = true;
    F[1] = 0;

    while (!todo.empty())
    {
        int next = todo.back();
        todo.pop_back();

        for (Edge e : V[next])
        {
            if (done[e.dest])
                continue;

            if (!e.fwd)
                F[1]++;
            done[e.dest] = true;
            todo.push_back(e.dest);
        }
    }

    todo.push_back(1);

    while (!todo.empty())
    {
        int next = todo.back();
        todo.pop_back();

        for (Edge e : V[next])
        {
            if (F[e.dest] != -1)
                continue;

            if (e.fwd)
                F[e.dest] = F[next] + 1;
            else
                F[e.dest] = F[next] - 1;

            todo.push_back(e.dest);
        }
    }

    int minf = INT_MAX;

    rep(i,1,n)
        chmin(minf, F[i]);

    cout << minf << endl;

    rep(i,1,n)
        if (F[i] == minf)
            cout << i << " ";
    cout << endl;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:05:02+00:00Added an answer on June 10, 2026 at 9:05 am

    I think that the following algorithm works correctly, and it certainly works in linear time.

    The motivation for this algorithm is the following. Let’s suppose that you already know the value of f(v) for some single node v. Now, consider any node u adjacent to v. If we want to compute the value of f(u), we can reuse some of the information from f(v) in order to compute it. Note that in order to get from any node w in the graph to u, one of two cases must happen:

    1. That path passes through the edge connecting u and v. In that case, the way that we get from w to u is to go from w to v, then to follow the edge from v to u.
    2. That path does not pass through the edge connecting u and v. In that case, the way that we get from w to u is the exact same way that we got from w to v, except that we stop as soon as we get to u.

    The reason that this observation is important is that it means that if we know the number of edges we’d flip to get from any node to v, we can easily modify it to get the set of edges that we’d flip to get from any node to u. Specifically, it’s going to be the same set of edges as before, except that we want to direct the edge connecting u and v so that it connects v to u rather than the other way around.

    If the edge from u to v is initially directed (u, v), then we have to flip all the normal edges we flipped to get every node pointing at v, plus one more edge to get v pointed back at u. Thus f(u) = f(v) + 1. Otherwise, if the edge is originally directed (v, u), then the set of edges that we’d flip would be the same as before (pointing everything at v), except that we wouldn’t flip the edge (v, u). Thus f(u) = f(v) – 1.

    Consequently, once we know the value of f for a single node v, we can compute it for each adjacent node u as follows:

    f(u) = f(v) + 1    if (u, v) is an edge.
    f(u) = f(v) - 1    otherwise
    

    This means that we can compute f(v) for all nodes v as follows:

    1. Compute f(v) for some initial node v, chosen arbitrarily.
    2. Do a DFS starting from v. When reaching a node u, compute its f score using the above logic.

    All that’s left to do is to compute f(v) for some initial node. To do this, we can run a DFS from v outward. Every time we see an edge pointed the wrong way, we have to flip it. Thus the initial value of f(v) is given by the number of wrong-pointing edges we find during the initial DFS.

    We thus can compute the f score for each node in O(n) time by doing an initial DFS to compute f(v) for the initial node, then a secondary DFS to compute f(u) for each other node u. You can then for-loop over each of the n f-scores to find the minimum score, then do one more loop to find all values with that f-score. Each of these steps takes O(n) time, so the overall algorithm takes O(n) time as well.

    Hope this helps! This was an awesome problem!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(This is derived from a recently completed programming competition) You are given two arrays
We can specify a derived from constraint on generic type parameters like this: class
I have a class derived from Dictionary. I need this class to simulate a
i have a class 'MyTextBox' that derives from the default TextBox in Silverlight. This
In the header, I'm defining bool isActive. In classes derived from this one, I
This question is derived from my previous SO question's commends . I am confused
I have a function in my class (derived from this function) to generate a
Twitter recently announced that you can approximate the rank of any given twitter user
This question is derived from: How to get this Method object via reflection? I'm
I have a class that derives from enable_shared_from_this ... (Recently been added to std

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.