Skip to content

Commit 4dab05d

Browse files
committed
[2700][1A] CF1617E 结论题 建模 树的直径
1 parent cea11f6 commit 4dab05d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

main/1600-1699/1617E.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
"math/bits"
8+
)
9+
10+
// github.com/EndlessCheng/codeforces-go
11+
func f(v, w uint) (c int) {
12+
for v != w {
13+
if v < w {
14+
v, w = w, v
15+
}
16+
v = 1<<bits.Len(v-1) - v
17+
c++
18+
}
19+
return
20+
}
21+
22+
func CF1617E(_r io.Reader, out io.Writer) {
23+
in := bufio.NewReader(_r)
24+
var n, p, q int
25+
Fscan(in, &n)
26+
a := make([]uint, n)
27+
for i := range a {
28+
Fscan(in, &a[i])
29+
}
30+
31+
mx := -1
32+
for i := 1; i < n; i++ {
33+
c := f(a[i], a[0])
34+
if c > mx {
35+
mx, p = c, i
36+
}
37+
}
38+
39+
mx = -1
40+
for i, v := range a {
41+
if i != p {
42+
c := f(v, a[p])
43+
if c > mx {
44+
mx, q = c, i
45+
}
46+
}
47+
}
48+
Fprint(out, p+1, q+1, mx)
49+
}
50+
51+
//func main() { CF1617E(os.Stdin, os.Stdout) }

main/1600-1699/1617E_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/1617/E
9+
// https://codeforces.com/problemset/status/1617/problem/E
10+
func TestCF1617E(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
5
15+
5 6 7 8 9
16+
outputCopy
17+
2 5 5
18+
inputCopy
19+
2
20+
4 8
21+
outputCopy
22+
1 2 2`
23+
testutil.AssertEqualCase(t, rawText, 0, CF1617E)
24+
}

0 commit comments

Comments
 (0)