Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem F:
There is No Alternative

  ICPC (Isles of Coral Park City) consist of several beautiful islands.

  The citizens requested construction of bridges between islands to resolve inconveniences of using boats between islands, and they demand that all the islands should be reachable from any other islands via one or more bridges.

  The city mayor selected a number of pairs of islands, and ordered a building company to estimate the costs to build bridges between the pairs. With this estimate, the mayor has to decide the set of bridges to build, minimizing the total construction cost.

  However, it is difficult for him to select the most cost-efficient set of bridges among those connecting all the islands. For example, three sets of bridges connect all the islands for the Sample Input 1. The bridges in each set are expressed by bold edges in Figure F.1.

Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1

  As the first step, he decided to build only those bridges which are contained in all the sets of bridges to connect all the islands and minimize the cost. We refer to such bridges as no alternative bridges. In Figure F.2, no alternative bridges are drawn as thick edges for the Sample Input 1, 2 and 3.

Figure F.2. No alternative bridges for Sample Input 1, 2 and 3

Write a program that advises the mayor which bridges are no alternative bridges for the given input.

Input

The input consists of a single test case.
  

$N$ $M$

$S_1$ $D_1$ $C_1$

.

.

.

$S_M$ $D_M$ $C_M$

The first line contains two positive integers $N$ and $M$. $N$ represents the number of islands and each island is identified by an integer 1 through $N$. $M$ represents the number of the pairs of islands between which a bridge may be built.

Each line of the next $M$ lines contains three integers $S_i$, $D_i$ and $C_i$ ($1 \leq i \leq M$) which represent that it will cost $C_i$ to build the bridge between islands $S_i$ and $D_i$. You may assume $3 \leq N \leq 500$, $N − 1 \leq M \leq min(50000, N(N − 1)/2)$, $1 \leq S_i < D_i \leq N$, and $1 \leq C_i \leq 10000$. No two bridges connect the same pair of two islands, that is, if $i \ne j$ and $S_i = S_j$, then $D_i \ne D_j$ . If all the candidate bridges are built, all the islands are reachable from any other islands via one or more bridges.

Output

Output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.

Sample Input 1

4 4
1 2 3
1 3 3
2 3 3
2 4 3

Sample Output 1

1 3

Sample Input 2

4 4
1 2 3
1 3 5
2 3 3
2 4 3

Sample Output 2

3 9

Sample Input 3

4 4
1 2 3
1 3 1
2 3 3
2 4 3

Sample Output 3

2 4

Sample Input 4

3 3
1 2 1
2 3 1
1 3 1

Sample Output 4

0 0



Sample input:
4 4
1 2 3
1 3 3
2 3 3
2 4 3


Sample output:
1 3

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
E0 = []
for i in range(M):
    S, D, C = map(int, input().split())
    E0.append((C, S, D))
E0.sort()

*parent, = range(N)
def root(x):
    if x == parent[x]:
        return x
    y = parent[x] = root(parent[x])
    return y
def unite(x, y):
    px = root(x); py = root(y)
    if px < py:
        parent[py] = px
    else:
        parent[px] = py
G = [[] for i in range(N)]
E1 = set()
cnt = 0; cost = 0; base = None
for i in range(M):
    C, S, D = E0[i]
    if root(S-1) != root(D-1):
        unite(S-1, D-1)
        cost += C
        cnt += 1
        if cnt == N-1:
            base = C
cnt = ans = 0
for C, S, D in E0:
    if C <= base:
        G[S-1].append(D-1)
        G[D-1].append(S-1)
        cnt += 1
        if C == base:
            E1.add((S-1, D-1))
        else:
            ans += C
def bridge(G, N):
    result = set()
    label = [0]*N
    cost = [0]*N
    def dfs(u, p):
        res = 0
        for v in G[u]:
            if v == p:
                continue
            if label[v]:
                cost[v] -= 1
                res += 1
            else:
                label[v] = 1
                r = dfs(v, u)
                if r == 0:
                    result.add((u, v) if u < v else (v, u))
                res += r
        res += cost[u]
        return res
    label[0] = 1
    dfs(0, -1)
    return result
B = bridge(G, N)
print(cnt - len(E1) + len(B & E1), ans + len(B & E1)*base)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
E0 = []
for i in range(M):
    S, D, C = map(int, input().split())
    E0.append((C, S, D))
E0.sort()

*parent, = range(N)
def root(x):
    if x == parent[x]:
        return x
    y = parent[x] = root(parent[x])
    return y
def unite(x, y):
    px = root(x); py = root(y)
    if px < py:
        parent[py] = px
    else:
        parent[px] = py
cnt = 0; base = 10**9
used = set()
for i in range(M):
    C, S, D = E0[i]
    if root(S-1) != root(D-1):
        unite(S-1, D-1)
        used.add((S-1, D-1))
        cnt += 1
        if cnt == N-1:
            base = C
ES = {}; CM = {}
for i in range(M):
    C, S, D = E0[i]
    if C <= base:
        ES.setdefault(C, set()).add((S-1, D-1))
        CM[S-1, D-1] = C
def bridge(G, N):
    result = set()
    label = [0]*N
    cost = [0]*N
    def dfs(u, p):
        res = 0
        for v in G[u]:
            if v == p:
                continue
            if label[v]:
                cost[v] -= 1
                res += 1
            else:
                label[v] = 1
                r = dfs(v, u)
                if r == 0:
                    result.add((u, v) if u < v else (v, u))
                res += r
        res += cost[u]
        return res
    for v in range(N):
        if not label[v]:
            label[v] = 1
            dfs(v, -1)
    return result
G0 = [[] for i in range(N)]
cnt = 0; ans = 0
for C in sorted(ES):
    G = [e[:] for e in G0]
    for S, D in ES[C]:
        G[S].append(D)
        G[D].append(S)
        if (S, D) in used:
            G0[S].append(D)
            G0[D].append(S)
    B = bridge(G, N)
    cnt += len(B & ES[C])
    ans += sum(map(CM.__getitem__, B & ES[C]))
print(cnt, ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
E0 = []
for i in range(M):
    S, D, C = map(int, input().split())
    E0.append((C, S-1, D-1))
E0.sort()

*parent, = range(N)
def root(x):
    if x == parent[x]:
        return x
    y = parent[x] = root(parent[x])
    return y
def unite(x, y):
    px = root(x); py = root(y)
    if px < py:
        parent[py] = px
    else:
        parent[px] = py
cnt = 0; base = None
for i in range(M):
    C, S, D = E0[i]
    if root(S) != root(D):
        unite(S, D)
        cnt += 1
        if cnt == N-1:
            base = C
ES = {}; CM = {}
for i in range(M):
    C, S, D = E0[i]
    if C <= base:
        ES.setdefault(C, set()).add((S, D))
        CM[S, D] = C
def bridge(G, N):
    result = set()
    label = [None]*N
    gen = 0
    cost = [0]*N
    def dfs(u, p):
        nonlocal gen
        res = 0
        for v in G[u]:
            if v == p:
                continue
            if label[v] is not None:
                if label[v] < label[u]:
                    cost[v] += 1
                    res += 1
            else:
                label[v] = gen; gen += 1
                r = dfs(v, u)
                if r == 0:
                    result.add((u, v) if u < v else (v, u))
                res += r
        res -= cost[u]
        return res
    for v in range(N):
        if not label[v]:
            label[v] = gen; gen += 1
            r = dfs(v, -1)
            assert r == 0, r
    return result
G = [[] for i in range(N)]
cnt = 0; ans = 0
for C in sorted(ES):
    for S, D in ES[C]:
        G[S].append(D)
        G[D].append(S)
    B = bridge(G, N)
    cnt += len(B & ES[C])
    ans += len(B & ES[C]) * C
print(cnt, ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

class UnionFind:
    def __init__(self, size):
        self.table = [-1 for _ in range(size)]

    def find(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]

    def union(self, x, y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            if self.table[s1] <= self.table[s2]:
                self.table[s1] += self.table[s2]
                self.table[s2] = s1
            else:
                self.table[s2] += self.table[s1]
                self.table[s1] = s2
            return True
        return False

    def subsetall(self):
        a = []
        for i in range(len(self.table)):
            if self.table[i] < 0:
                a.append((i, -self.table[i]))
        return a

def main():
    rr = []

    def f(n,m):
        ee = [LI() + [_] for _ in range(m)]
        e = sorted(ee, key=lambda x: [x[2], x[3]])
        s = []
        ns = []
        uf = UnionFind(n+1)
        t = 0
        ttc = 0
        for a,b,c,i in e:
            if uf.union(a,b):
                s.append(i)
                t += c
                ttc += 1
            else:
                ns.append((a,b,c))

        r = 0
        rc = 0
        for si in s:
            tr = 0
            tc = 0
            uf = UnionFind(n+1)
            w = ee[si][2]
            for sj in s:
                if si == sj:
                    continue
                uf.union(ee[sj][0],ee[sj][1])
            sf = True
            for a,b,c in ns:
                if c == w and uf.union(a,b):
                    sf = False
                    break
            if sf:
                rc += 1
                r += w

        return '{} {}'.format(rc, r)

    while 1:
        n,m = LI()
        if n == 0:
            break
        rr.append(f(n,m))
        break

    return '\n'.join(map(str,rr))


print(main())