dfs and similar graphs trees *1300

Please click on ads to support us..

Python Code:

n = int(input())

tree = {}
node_count_edges = {}
u, v = map(int, input().split())
tree[u] = [v]
tree[v] = [u]
root = u
for i in range(n - 2):
    u, v = map(int, input().split())
    if u not in tree: tree[u] = []
    if v not in tree: tree[v] = []
    tree[u].append(v)
    tree[v].append(u)

curr_nodes = [root]
l = 0
r = 0
i = 0
node_side = {}
passed = set()
while len(curr_nodes) != 0:
    new_curr_nodes = []
    for node in curr_nodes:
        if node in passed:
            continue
        passed.add(node)
        new_curr_nodes.extend(tree[node])
        if i % 2 == 0:
            node_side[node] = 'left'
        else:
            node_side[node] = 'right'

        if i % 2 == 0:
            l += 1
        else:
            r += 1

    i += 1
    curr_nodes = new_curr_nodes


ans = 0
for node in tree:
    if node_side[node] == 'left':
        ans += r - len(tree[node])
    else:
        ans += l - len(tree[node])

print(ans // 2)

C++ Code:

/*亲爱的上帝,我求你给我力量来赢得这场战斗。*/
#include<bits/stdc++.h> 
using namespace std;
using i64 = long long;
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define vl vector<i64>
#define vi vector<int>
template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}

constexpr int mxN = 2e5;
vector<vector<int>> A(mxN);
vector<bool> vis(mxN), color(mxN, 0);
void solve() {
    int N;
    cin >> N;
    for(int i=0; i<N-1; i++) {
        int u, v;
        cin >> u >> v;
        A[--u].push_back(--v);
        A[v].push_back(u);
    }
    i64 ONE = 0;
    function<void(int, int)> dfs = [&](int x, int col) {
        vis[x] = 1;
        color[x] = col;
        if(color[x]) ONE++;
        for(int j:A[x]) {
            if(!vis[j]) {
                dfs(j, col ^ 1);
            } 
        }

    };
   
    for(int i=0; i<N; i++) {
        if(!vis[i]) {
            dfs(i, 1);
        }
    }
    cout << ONE *1LL* (N - ONE) - N + 1;
    
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int TT=1; 
    // cin >> TT;
    while(TT--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST