結果

問題 No.2277 Honest or Dishonest ?
ユーザー ccppjsrbccppjsrb
提出日時 2023-04-21 22:07:03
言語 Go
(1.22.1)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 5,857 bytes
コンパイル時間 16,171 ms
コンパイル使用メモリ 233,352 KB
実行使用メモリ 17,024 KB
最終ジャッジ日時 2024-11-08 06:33:05
合計ジャッジ時間 19,502 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 59 ms
14,464 KB
testcase_04 AC 50 ms
14,336 KB
testcase_05 AC 23 ms
6,912 KB
testcase_06 AC 40 ms
12,544 KB
testcase_07 AC 36 ms
10,752 KB
testcase_08 AC 21 ms
6,656 KB
testcase_09 AC 33 ms
9,984 KB
testcase_10 AC 26 ms
8,576 KB
testcase_11 AC 12 ms
5,248 KB
testcase_12 AC 19 ms
8,064 KB
testcase_13 AC 46 ms
13,824 KB
testcase_14 AC 25 ms
8,960 KB
testcase_15 AC 19 ms
6,016 KB
testcase_16 AC 37 ms
10,624 KB
testcase_17 AC 17 ms
6,016 KB
testcase_18 AC 50 ms
13,312 KB
testcase_19 AC 51 ms
13,696 KB
testcase_20 AC 41 ms
11,776 KB
testcase_21 AC 35 ms
11,264 KB
testcase_22 AC 32 ms
10,240 KB
testcase_23 AC 40 ms
14,080 KB
testcase_24 AC 29 ms
9,344 KB
testcase_25 AC 35 ms
10,880 KB
testcase_26 AC 13 ms
5,632 KB
testcase_27 AC 21 ms
7,936 KB
testcase_28 AC 19 ms
6,144 KB
testcase_29 AC 28 ms
8,192 KB
testcase_30 AC 21 ms
7,296 KB
testcase_31 AC 34 ms
10,752 KB
testcase_32 AC 26 ms
7,680 KB
testcase_33 AC 49 ms
13,568 KB
testcase_34 AC 46 ms
13,824 KB
testcase_35 AC 7 ms
5,248 KB
testcase_36 AC 33 ms
10,240 KB
testcase_37 AC 44 ms
14,464 KB
testcase_38 AC 10 ms
5,248 KB
testcase_39 AC 14 ms
6,016 KB
testcase_40 AC 8 ms
5,248 KB
testcase_41 AC 52 ms
15,360 KB
testcase_42 AC 35 ms
11,008 KB
testcase_43 AC 55 ms
16,256 KB
testcase_44 AC 62 ms
16,896 KB
testcase_45 AC 63 ms
17,024 KB
testcase_46 AC 57 ms
17,024 KB
testcase_47 AC 60 ms
16,896 KB
testcase_48 AC 63 ms
17,024 KB
testcase_49 AC 57 ms
16,768 KB
testcase_50 AC 57 ms
16,000 KB
testcase_51 AC 54 ms
15,488 KB
testcase_52 AC 55 ms
16,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var iost *Iost

type Iost struct {
	Scanner *bufio.Scanner
	Writer  *bufio.Writer
}

func NewIost(fp io.Reader, wfp io.Writer) *Iost {
	const BufSize = 2000005
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, BufSize), BufSize)
	return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)}
}
func (i *Iost) Text() string {
	if !i.Scanner.Scan() {
		panic("scan failed")
	}
	return i.Scanner.Text()
}
func (i *Iost) Atoi(s string) int                 { x, _ := strconv.Atoi(s); return x }
func (i *Iost) GetNextInt() int                   { return i.Atoi(i.Text()) }
func (i *Iost) Atoi64(s string) int64             { x, _ := strconv.ParseInt(s, 10, 64); return x }
func (i *Iost) GetNextInt64() int64               { return i.Atoi64(i.Text()) }
func (i *Iost) Atof64(s string) float64           { x, _ := strconv.ParseFloat(s, 64); return x }
func (i *Iost) GetNextFloat64() float64           { return i.Atof64(i.Text()) }
func (i *Iost) Print(x ...interface{})            { fmt.Fprint(i.Writer, x...) }
func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) }
func (i *Iost) Println(x ...interface{})          { fmt.Fprintln(i.Writer, x...) }
func isLocal() bool                               { return os.Getenv("NICKEL") == "BACK" }
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	if isLocal() {
		fp, _ = os.Open(os.Getenv("WELL_EVERYBODY_LIES_TOO_MUCH"))
	}
	iost = NewIost(fp, wfp)
	defer func() {
		iost.Writer.Flush()
	}()
	solve()
}
func solve() {
	SetMod(Mod998244353)
	n := iost.GetNextInt()
	q := iost.GetNextInt()
	aa := make([]int, q)
	bb := make([]int, q)
	cc := make([]int, q)
	ee := make([][][2]int, n)
	for i := 0; i < q; i++ {
		aa[i] = iost.GetNextInt() - 1
		bb[i] = iost.GetNextInt() - 1
		cc[i] = iost.GetNextInt()
		ee[aa[i]] = append(ee[aa[i]], [2]int{bb[i], cc[i]})
		ee[bb[i]] = append(ee[bb[i]], [2]int{aa[i], cc[i]})
	}
	visited := make([]bool, n)
	lier := make([]int, n)
	g := 0
	for i := 0; i < n; i++ {
		if visited[i] {
			continue
		}
		g++
		visited[i] = true
		q := make([]int, 0)
		q = append(q, i)
		for len(q) > 0 {
			p := q[0]
			q = q[1:]
			for _, e := range ee[p] {
				if visited[e[0]] {
					if lier[e[0]] != lier[p]^e[1] {
						iost.Println(0)
						return
					}
					continue
				}
				visited[e[0]] = true
				lier[e[0]] = lier[p] ^ e[1]
				q = append(q, e[0])
			}
		}
	}
	iost.Println(Mint(2).Pow(Mint(g)))
}

// Dsu Data structures and algorithms for disjoint set union problems
type Dsu struct {
	n            int
	parentOrSize []int
}

// NewDsu Constructor
func NewDsu(n int) *Dsu {
	p := make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = -1
	}
	return &Dsu{parentOrSize: p, n: n}
}

// Merge adds an edge (a, b).
func (d *Dsu) Merge(a, b int, meld ...func(int, int)) int {
	x := d.Leader(a)
	y := d.Leader(b)
	if x == y {
		return x
	}
	if -d.parentOrSize[x] < -d.parentOrSize[y] {
		x, y = y, x
	}
	d.parentOrSize[x] += d.parentOrSize[y]
	d.parentOrSize[y] = x
	for _, f := range meld {
		f(x, y)
	}
	return x
}

// Same returns whether the vertices a and b are in the same connected component.
func (d *Dsu) Same(a, b int) bool {
	return d.Leader(a) == d.Leader(b)
}

// Leader returns the representative of the connected component that contains the vertex a.
func (d *Dsu) Leader(a int) int {
	if d.parentOrSize[a] < 0 {
		return a
	}
	d.parentOrSize[a] = d.Leader(d.parentOrSize[a])
	return d.parentOrSize[a]
}

// Size returns the size of the connected component that contains the vertex a.
func (d *Dsu) Size(a int) int {
	return -d.parentOrSize[d.Leader(a)]
}

// Groups divides the graph into connected components and returns the list of them.
func (d *Dsu) Groups() [][]int {
	result := make([][]int, d.n)
	groups := make([][]int, 0)
	for i := 0; i < d.n; i++ {
		l := d.Leader(i)
		result[l] = append(result[l], i)
	}
	for i := 0; i < d.n; i++ {
		if result[i] == nil {
			continue
		}
		groups = append(groups, result[i])
	}
	return groups
}

// Mod constants.
const (
	Mod1000000007 = 1000000007
	Mod998244353  = 998244353
)

var (
	mod  Mint
	fmod func(Mint) Mint
)

// Mint treats the modular arithmetic
type Mint int64

// SetMod sets the mod. It must be called first.
func SetMod(newmod Mint) {
	switch newmod {
	case Mod1000000007:
		fmod = staticMod1000000007
	case Mod998244353:
		fmod = staticMod998244353
	default:
		mod = newmod
		fmod = dynamicMod
	}
}
func dynamicMod(m Mint) Mint {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}
func staticMod1000000007(m Mint) Mint {
	m %= Mod1000000007
	if m < 0 {
		return m + Mod1000000007
	}
	return m
}
func staticMod998244353(m Mint) Mint {
	m %= Mod998244353
	if m < 0 {
		return m + Mod998244353
	}
	return m
}

// Mod returns m % mod.
func (m Mint) Mod() Mint {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Mint) Inv() Mint {
	return m.Pow(Mint(0).Sub(2))
}

// Pow returns m^n
func (m Mint) Pow(n Mint) Mint {
	p := Mint(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Mint) Add(x Mint) Mint {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Mint) Sub(x Mint) Mint {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Mint) Mul(x Mint) Mint {
	return (m * x).Mod()
}

// Div returns m/x
func (m Mint) Div(x Mint) Mint {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Mint) AddAs(x Mint) *Mint {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Mint) SubAs(x Mint) *Mint {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Mint) MulAs(x Mint) *Mint {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Mint) DivAs(x Mint) *Mint {
	*m = m.Div(x)
	return m
}
0