Sort by

recency

|

58 Discussions

|

  • + 0 comments

    I wondered for a very long time what was wrong with my reasoning simply because not every single variable was a long long int penalty kick online. Please ensure that all variables are specified as long long ints without exception if you are experiencing issues with test cases failing.

  • + 0 comments
    from decimal import Decimal
    class DisjointSet:
        def __init__(self):
            self.parent=self
            self.size=1
        def findParent(self):
            if self.parent!=self:
                self.parent=self.parent.findParent()
            return self.parent
        def union(self,other):
            if self==other:
                return
            root=self.findParent()
            other_root=other.findParent()
            if root==other_root:
                return
            if root.size>other_root.size:
                other_root.parent=root
                root.size+=other_root.size
            else:
                root.parent=other_root
                other_root.size+=root.size
    def nc2(n):
        if n<2:
            return 0
        return Decimal(n*(n-1)/2)
    def nc3(n):
        if n<3:
            return 0
        return Decimal(n*(n-1)*(n-2)/6)
    
    n=int(input())
    components=[None]*n
    for i in range(n-1):
        a,b,c=input().split()
        a=int(a)-1
        b=int(b)-1
        if c=='r':
            continue
        if not components[a]:
            components[a]=DisjointSet()
        if not components[b]:
            components[b]=DisjointSet()
        components[a].union(components[b])
    
    
    
    uniqueComponents=set()
    for x in components:
        if x:
            uniqueComponents.add(x.findParent())
    valid_triplets=Decimal(nc3(n))
    
    for x in uniqueComponents:
        valid_triplets-=nc3(x.size)
        valid_triplets-=nc2(x.size)*(n-x.size)
    print(int(valid_triplets)%(10**9+7))
    
  • + 0 comments

    In Go. Brute Force.

    Got a couple test cases right. But of course, it times out.

    // Hackerrank 2022-06-15 task (GO) Kundu and Tree.txt
    // Made clever wrong non-brute force solutions 6 days
    
    package main
    
    import (
    	"fmt"
    	"io/ioutil"
    	"os"
    )
    
    func main() {
    	// graph vertex red adjacency and black adjacency lists
    	var ra, ba [][]int
    
    	// stdin, parse, fill ra, ba
    	bytes, err := ioutil.ReadAll(os.Stdin)
    	if err != nil {
    		panic(err)
    	}
    	var accu int
    	var inDigits bool
    	var n int
    	var one int
    	var two int
    	for _, b := range bytes {
    		if b >= 48 && b <= 57 {
    			inDigits = true
    			accu = accu*10 + int(b) - 48
    		} else {
    			if inDigits {
    				one = two
    				two = accu
    				accu = 0
    				inDigits = false
    				if n == 0 {
    					n = two
    					// slot[0] is never used. Index [1..n].
    					ra = make([][]int, n+1)
    					ba = make([][]int, n+1)
    					continue
    				}
    			}
    			switch b {
    			case byte('b'):
    				ba[one] = append(ba[one], two)
    				ba[two] = append(ba[two], one)
    				break
    			case byte('r'):
    				ra[one] = append(ra[one], two)
    				ra[two] = append(ra[two], one)
    				break
    			}
    		}
    	}
    
    	// Discovered-After-Red Lists
    	darl := make([][]bool, n+1)
    
    	// Do BFS from each vertex
    	for s := 1; s <= n; s++ {
    		// discovered
    		d := make([]bool, n+1)
    		// discovered after red
    		dar := make([]bool, n+1)
    		// fifo
    		f := make([]int, 0)
    		// fifo after red
    		far := make([]bool, 0)
    		// queue the search key
    		d[s] = true
    		f = append(f, s)
    		far = append(far, false)
    		// run until dry
    		for len(f) > 0 {
    			// dequeue vertex
    			v := f[0]
    			f = f[1:]
    			// dequeue after red?
    			ar := far[0]
    			far = far[1:]
    			for _, u := range ba[v] {
    				if !d[u] {
    					d[u] = true
    					dar[u] = ar
    					f = append(f, u)
    					far = append(far, ar)
    				}
    			}
    			for _, u := range ra[v] {
    				if !d[u] {
    					d[u] = true
    					dar[u] = true
    					f = append(f, u)
    					far = append(far, true)
    				}
    			}
    		}
    		darl[s] = dar
    	}
    
    	// test the selftest
    	// good, caught it: darl[3][7] = !darl[7][3]
    	// as a selftest, prove the symmetry of darl
    	// for i := 1; i <= n; i++ {
    	// 	if darl[i][i] {
    	// 		fmt.Println("darl[i][i]")
    	// 	}
    	// 	for j := i + 1; j <= n; j++ {
    	// 		if darl[i][j] != darl[j][i] {
    	// 			fmt.Println("asym")
    	// 		}
    	// 	}
    	// }
    
    	// Okay, I brute-forced it to here. Now what?
    
    	N := int64(0)
    	for i := 1; i <= n; i++ {
    		for j := i + 1; j <= n; j++ {
    			if darl[i][j] {
    				for k := j + 1; k <= n; k++ {
    					if darl[i][k] && darl[j][k] {
    						N++
    					}
    				}
    			}
    		}
    	}
    	fmt.Println(N)
    }
    
  • + 0 comments

    solved using permutation and combination , explaning with an example 8 1 2 r 2 3 b 3 4 b 4 5 b 5 6 r 6 7 b 7 8 r

    lets say there are n nodes

    First find groups of nodes with direct connected black edges, so for above example

    multi_edge_grp = 1 group with 4 nodes :- 2,3,4,5 (lets say there are q groups with diff nodes count (qc))

    single_edge_grp = 1 group with 2 nodes :- 6,7 (lets say there are p groups with 2 nodes)

    calculations:-

    total_no_of_tuples = nC3

    single_edge_grp_invalid_tuple_count = p * (n - 2)C1

    multi_edge_grp_invalid_tuple_count = Σ(over q) [ qcC2 * (n - 2)C1 - qcC2 * (qc - 2)C1 + qcC3]

    total_valid_tuples = total_no_of_tuples - (single_edge_grp_invalid_tuple_count + multi_edge_grp_invalid_tuple_count)

  • + 0 comments

    Simple and concise solution encapsulating the ideas given by others:

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include<stack>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    long long int parent(long long int i, long long int* arr){
        while(arr[i]>0)
        i=arr[i];
        return i;
    }
    long long int bin(long long int a){
        if(a<3)
        return 0;
        return ((a)*(a-1)*(a-2))/6;
    }
    int main() { 
        long long int n;
        cin>>n;
        long long int parentarr[n+1];
        for(long long int i=0;i<n+1;i++)
        parentarr[i]=-1;
        for(long long int i=1;i<n;i++){
            long long int j,k;
            char col;
            cin>>j>>k>>col;
            long long int x=parent(j,parentarr);
            long long int y=parent(k,parentarr);
            if(x!=y&&col=='b'){
                    long long int c=parentarr[x]<=parentarr[y]?x:y;
                    long long int d=parentarr[x]>parentarr[y]?x:y;
                    parentarr[c]+=parentarr[d];
                    parentarr[d]=c;
            } 
        }
        long long int total_triplets=bin(n);
        long long int to_subtract=0;
        for(long long int i=1;i<n+1;i++){
            if(parentarr[i]>=-1)
            continue;
            long long int z=abs(parentarr[i]);
            to_subtract+=bin(z)+(n-z)*((z*(z-1))/2);
        }
        long long int sums=total_triplets-to_subtract;
        cout<<sums%(1000000007)<<endl;
        return 0;
    }