QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#537425#7860. Graph of Maximum Degree 3JEdwardTL 76ms27412kbC++203.4kb2024-08-30 13:29:192024-08-30 13:29:20

Judging History

你现在查看的是最新测评结果

  • [2024-08-30 13:29:20]
  • 评测
  • 测评结果:TL
  • 用时:76ms
  • 内存:27412kb
  • [2024-08-30 13:29:19]
  • 提交

answer

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define int long long
#define endl '\n'
#define lowbit(x) (x)&(-x)
#define pii pair<int,int>
#define pb push_back
#define all(s) s.begin(), s.end()
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define here system("pause")
using namespace std;
template <class T> inline void read(T& x) { x = 0; char c = getchar(); bool f = 0; for (; !isdigit(c); c = getchar()) f ^= (c == '-'); for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); x = f ? -x : x; }
template <class T> inline void write(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else write(x / 10), putchar(x % 10 + 48); }
template<typename T> void chkmin(T& lhs, const T& rhs) { lhs = lhs > rhs ? rhs : lhs; }
template<typename T> void chkmax(T& lhs, const T& rhs) { lhs = lhs < rhs ? rhs : lhs; }

const int N = 1e5+5;
const int mod = 998244353;
const int INF = 8e18+7;
const double eps = 1e-6;
inline int pow(int a, int b, int p){ int res = 1%p; while(b){ if(b&1) res=res*a%p; a=a*a%p, b>>=1;} return res;}
inline int inv(int a, int p){ return pow(a,p-2,p)%p;}
mt19937_64 rnd(chrono::duration_cast< chrono::nanoseconds>(chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
	uniform_int_distribution<int> dist(L, R);
	return dist(rnd);
}
using ull = unsigned long long;

int n, m;
set<int> r[N], b[N];
map<array<int, 4>, int> four;
bool vis[N];
inline bool in(int x, vector<int> &vec){
	for(auto t : vec){
		if(t == x) return 1;
	}
	return 0;
}
inline void dfs(int x, int fa, vector<int> &vec){
	vis[x] = 1;
	vec.push_back(x);
	for(int y : r[x]){
		if(y == fa || vis[y] || in(y, vec)) continue;
		dfs(y, x, vec);
	}
	for(int y : b[x]){
		if(y == fa || vis[y] || in(y, vec)) continue;
		dfs(y, x, vec);
	}
}

inline int check(vector<int> &vec){
	int now = vec[0];
	queue<int> red, blue;
	red.push(now), blue.push(now);
	map<int, int> mp1, mp2;
	
	while(red.size()){
		int u = red.front();
		mp1[u] = 1;
		red.pop();
		for(int v : r[u]){
			if(in(v, vec) && !mp1.count(v)){
				red.push(v);
			}
		}
	}
	
	while(blue.size()){
		int u = blue.front();
		mp2[u] = 1;
		blue.pop();
		for(int v : b[u]){
			if(in(v, vec) && !mp2.count(v)){
				blue.push(v);
			}
		}
	}
	
	if(mp1.size() == 4 && mp2.size() == 4) return 1;
	else return 0;
}

inline void sol(){
	cin >> n >> m;
	for(int i=1;i<=m;i++){
		int u, v, c;
		cin >> u >> v >> c;
		if(c == 1) r[u].insert(v), r[v].insert(u);
		if(c == 0) b[u].insert(v), b[v].insert(u);
	}
	
	int ans = 0;
	for(int u=1;u<=n;u++){
		for(auto v : r[u]){
			if(b[u].count(v)){
				++ans;
				for(int t : r[u]){
					if(t == v) continue;
					if(b[v].count(t)) ++ans;
				}
				for(int t : b[u]){
					if(t == v) continue;
					if(r[v].count(t)) ++ans;
				}
			}
		}
	}
	
	ans >>= 1;
	ans += n;
	ans %= mod;
	for(int i=1;i<=n;i++){
		if(!vis[i]){
			vector<int> tmp {};
			dfs(i, 0, tmp);
			
			if(tmp.size() == 4){
				ans += check(tmp);
			}
		}
	}
	
	cout << ans%mod << endl;
}


signed main(){
#ifdef LOCAL
	clock_t CLOCK_S = clock();
	freopen("1.in", "r", stdin);
#endif
	IOS;
	int tc = 1;
//	cin >> tc;
	while(tc--){
		sol();
	}
#ifdef LOCAL
	clock_t CLOCK_E = clock();
	cerr << "Judging Time: " << 1.00 * (CLOCK_E - CLOCK_S) / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 12952kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 0ms
memory: 13008kb

input:

4 6
1 2 0
2 3 0
3 4 0
1 4 1
2 4 1
1 3 1

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 3ms
memory: 12956kb

input:

20 28
9 6 1
9 6 0
3 8 0
8 4 0
3 8 1
3 4 1
2 13 0
13 1 0
19 1 0
2 1 1
2 19 1
13 19 1
14 15 1
14 15 0
7 12 0
12 17 0
20 17 0
7 17 1
7 20 1
12 20 1
16 18 0
18 10 0
5 10 0
16 10 1
16 5 1
18 5 1
4 6 0
9 11 0

output:

27

result:

ok 1 number(s): "27"

Test #4:

score: 0
Accepted
time: 0ms
memory: 13260kb

input:

100 150
93 23 0
23 81 0
76 81 0
93 81 1
93 76 1
23 76 1
100 65 0
65 56 0
19 56 0
100 56 1
100 19 1
65 19 1
2 98 0
2 98 1
26 63 0
63 90 0
26 63 1
26 90 1
6 11 0
11 67 0
6 11 1
6 67 1
37 89 0
89 64 0
25 64 0
37 64 1
37 25 1
89 25 1
84 10 0
10 29 0
75 29 0
84 29 1
84 75 1
10 75 1
7 70 1
7 70 0
28 92 0
...

output:

141

result:

ok 1 number(s): "141"

Test #5:

score: 0
Accepted
time: 59ms
memory: 25884kb

input:

100000 133680
36843 86625 0
86625 63051 0
35524 63051 0
36843 63051 1
36843 35524 1
86625 35524 1
55797 82715 0
55797 82715 1
70147 35104 0
35104 91732 0
70147 35104 1
70147 91732 1
94917 70395 0
70395 68250 0
24100 68250 0
94917 68250 1
94917 24100 1
70395 24100 1
83033 18450 1
83033 18450 0
34462 ...

output:

144604

result:

ok 1 number(s): "144604"

Test #6:

score: 0
Accepted
time: 58ms
memory: 25560kb

input:

100000 133388
86620 74346 0
74346 19047 0
54911 19047 0
86620 19047 1
86620 54911 1
74346 54911 1
23715 93094 0
93094 91208 0
63189 91208 0
23715 91208 1
23715 63189 1
93094 63189 1
99337 41426 1
99337 41426 0
83742 45546 0
45546 73862 0
83742 45546 1
83742 73862 1
85256 2812 0
2812 59368 0
85918 59...

output:

144348

result:

ok 1 number(s): "144348"

Test #7:

score: 0
Accepted
time: 74ms
memory: 27168kb

input:

100000 150000
86541 24385 0
24385 75745 0
52353 75745 0
86541 75745 1
86541 52353 1
24385 52353 1
89075 78015 0
89075 78015 1
52519 74846 0
74846 12045 0
73265 12045 0
52519 12045 1
52519 73265 1
74846 73265 1
17884 63159 0
63159 47308 0
56073 47308 0
17884 47308 1
17884 56073 1
63159 56073 1
72134 ...

output:

144639

result:

ok 1 number(s): "144639"

Test #8:

score: 0
Accepted
time: 71ms
memory: 27168kb

input:

100000 150000
91951 68612 1
91951 68612 0
18361 92673 0
92673 52678 0
86520 52678 0
18361 52678 1
18361 86520 1
92673 86520 1
58779 2421 0
58779 2421 1
66622 6461 0
6461 96943 0
66622 6461 1
66622 96943 1
27201 480 1
27201 480 0
19082 3895 0
3895 17796 0
3117 17796 0
19082 17796 1
19082 3117 1
3895 ...

output:

144471

result:

ok 1 number(s): "144471"

Test #9:

score: 0
Accepted
time: 73ms
memory: 27172kb

input:

100000 150000
43756 3552 0
3552 90269 0
43756 3552 1
43756 90269 1
11104 36935 1
11104 36935 0
11648 5480 0
5480 45320 0
11648 5480 1
11648 45320 1
19216 85746 0
19216 85746 1
68825 11173 0
11173 43155 0
68825 11173 1
68825 43155 1
27349 75259 0
27349 75259 1
1704 24478 0
24478 5980 0
1704 24478 1
1...

output:

144217

result:

ok 1 number(s): "144217"

Test #10:

score: 0
Accepted
time: 67ms
memory: 27140kb

input:

99999 149998
51151 43399 0
51151 43399 1
45978 28343 0
28343 9008 0
85724 9008 0
45978 9008 1
45978 85724 1
28343 85724 1
79446 12915 0
12915 65925 0
28869 65925 0
79446 65925 1
79446 28869 1
12915 28869 1
82642 95556 0
95556 68817 0
68334 68817 0
82642 68817 1
82642 68334 1
95556 68334 1
61212 7638...

output:

144219

result:

ok 1 number(s): "144219"

Test #11:

score: 0
Accepted
time: 71ms
memory: 27412kb

input:

100000 149999
26736 28785 0
28785 37945 0
26736 28785 1
26736 37945 1
1240 74368 0
74368 45022 0
1240 74368 1
1240 45022 1
40673 1276 0
1276 56395 0
40673 1276 1
40673 56395 1
35181 63341 0
63341 35131 0
60120 35131 0
35181 35131 1
35181 60120 1
63341 60120 1
99363 36973 0
99363 36973 1
85717 77683 ...

output:

144380

result:

ok 1 number(s): "144380"

Test #12:

score: 0
Accepted
time: 76ms
memory: 27072kb

input:

100000 150000
63695 11044 0
11044 34978 0
56531 34978 0
63695 34978 1
63695 56531 1
11044 56531 1
72139 3715 0
3715 21024 0
96696 21024 0
72139 21024 1
72139 96696 1
3715 96696 1
54670 49014 0
54670 49014 1
7670 61055 0
61055 38409 0
7670 61055 1
7670 38409 1
83399 50676 0
50676 98893 0
60069 98893 ...

output:

144559

result:

ok 1 number(s): "144559"

Test #13:

score: 0
Accepted
time: 0ms
memory: 12952kb

input:

1 0

output:

1

result:

ok 1 number(s): "1"

Test #14:

score: 0
Accepted
time: 2ms
memory: 13040kb

input:

100000 0

output:

100000

result:

ok 1 number(s): "100000"

Test #15:

score: 0
Accepted
time: 72ms
memory: 27396kb

input:

100000 150000
95066 31960 0
31960 89758 0
10935 89758 0
95066 89758 1
95066 10935 1
31960 10935 1
48016 97823 0
97823 10871 0
23454 10871 0
48016 10871 1
48016 23454 1
97823 23454 1
73749 35525 0
35525 54232 0
42182 54232 0
73749 54232 1
73749 42182 1
35525 42182 1
75405 71341 0
71341 70032 0
3284 7...

output:

125000

result:

ok 1 number(s): "125000"

Test #16:

score: 0
Accepted
time: 0ms
memory: 13244kb

input:

4 6
1 2 0
1 2 1
1 3 0
2 4 1
3 4 0
3 4 1

output:

7

result:

ok 1 number(s): "7"

Test #17:

score: 0
Accepted
time: 48ms
memory: 24012kb

input:

99998 115940
40840 40839 0
28249 28248 0
24785 24783 0
36536 36534 1
71904 71901 1
62023 62021 0
34737 34740 1
18430 18434 0
27506 27505 1
4665 4664 1
36578 36577 1
99311 99314 1
43484 43482 0
26457 26459 1
99698 99695 0
10170 10172 1
98176 98179 1
47786 47785 1
56529 56531 1
86896 86895 1
78204 782...

output:

104913

result:

ok 1 number(s): "104913"

Test #18:

score: 0
Accepted
time: 61ms
memory: 25192kb

input:

99996 126880
57665 57662 0
73031 73028 0
78744 78741 1
36913 36914 0
88139 88138 1
89276 89278 0
66433 66436 1
91069 91070 0
63929 63930 0
89625 89627 0
56400 56399 1
69226 69223 1
88433 88432 1
43807 43810 0
37146 37145 0
43789 43792 1
68123 68124 1
17957 17954 1
82804 82805 0
59808 59804 1
73840 7...

output:

103597

result:

ok 1 number(s): "103597"

Test #19:

score: 0
Accepted
time: 51ms
memory: 25196kb

input:

99996 128661
40089 40092 1
43861 43862 1
75629 75628 0
19597 19598 0
15151 15154 0
95642 95641 0
80320 80317 1
57255 57254 0
35316 35314 0
44675 44676 1
38847 38850 0
50886 50883 1
7617 7615 0
52310 52311 0
71474 71478 1
60036 60035 1
12009 12012 1
72347 72348 1
80343 80345 0
58804 58806 1
11386 113...

output:

103531

result:

ok 1 number(s): "103531"

Test #20:

score: 0
Accepted
time: 39ms
memory: 23320kb

input:

85086 109171
68997 68998 1
24077 24074 0
81830 81829 0
6102 6100 0
16851 16850 0
44103 44101 0
35639 35637 0
46162 46161 1
70373 70372 1
2625 2624 0
50990 50989 0
52220 52219 1
3452 3453 0
21915 21916 0
19561 19564 1
2616 2615 1
59039 59040 1
72589 72590 1
40147 40148 0
83359 83360 1
4274 4275 1
736...

output:

96534

result:

ok 1 number(s): "96534"

Test #21:

score: 0
Accepted
time: 0ms
memory: 13008kb

input:

6 9
1 2 0
1 2 1
1 3 0
2 3 1
3 4 0
4 5 0
4 6 1
5 6 0
5 6 1

output:

10

result:

ok 1 number(s): "10"

Test #22:

score: 0
Accepted
time: 50ms
memory: 24172kb

input:

99998 115940
91307 35051 0
41850 19274 0
35587 78894 0
26695 91651 1
79179 482 1
26680 7283 0
51999 18100 1
97541 51977 0
31565 24059 1
48770 33590 1
79885 37272 1
16578 79254 1
23825 66223 0
51722 3968 1
30481 33229 0
86577 14556 1
63261 87530 1
17567 19857 1
48438 12110 1
68610 47458 1
88373 92315...

output:

104913

result:

ok 1 number(s): "104913"

Test #23:

score: 0
Accepted
time: 64ms
memory: 24996kb

input:

99996 126880
31926 32431 0
89751 77638 0
81312 90949 1
9164 78061 0
79960 37357 1
15044 53165 0
46804 58840 1
96661 32396 0
93436 39774 0
81650 97489 0
28285 25380 1
51642 75847 1
38686 99309 1
65477 46389 0
17012 64436 0
39535 20467 1
55466 34797 1
56580 52438 1
88447 46598 0
94878 81598 1
36359 71...

output:

103597

result:

ok 1 number(s): "103597"

Test #24:

score: 0
Accepted
time: 66ms
memory: 25396kb

input:

99996 128661
68631 18634 1
39185 98747 1
93688 3993 0
63831 49896 0
88466 11249 0
76247 13150 0
44166 89827 1
14706 98796 0
55609 32463 0
96040 11481 1
15800 28436 0
35644 61568 1
90823 7941 0
16497 32517 0
70520 2507 1
36824 37963 1
43899 12185 1
16439 35062 1
22697 5663 0
22986 20940 1
93694 62377...

output:

103531

result:

ok 1 number(s): "103531"

Test #25:

score: 0
Accepted
time: 53ms
memory: 23492kb

input:

85086 109171
54967 52668 1
64243 48915 0
78737 27043 0
69272 84477 0
11191 72192 0
56490 36228 0
52083 25417 0
58946 51014 1
57855 26735 1
83625 46445 0
72878 43133 0
77230 69968 1
7791 38318 0
14928 27213 0
5215 50302 1
75864 25928 1
11582 54867 1
53793 83950 1
70191 16278 0
69499 3665 1
45931 3663...

output:

96534

result:

ok 1 number(s): "96534"

Test #26:

score: -100
Time Limit Exceeded

input:

100000 150000
99933 55358 0
90416 2554 0
64997 12630 0
43499 35304 0
43164 38359 0
82333 47941 0
15092 76350 1
6401 82373 0
90467 57736 1
72290 58218 0
64844 79192 0
71055 40232 1
54743 65698 0
19204 38062 1
1490 24882 0
18848 1970 1
18829 25405 0
93396 54676 1
5241 60149 0
26699 39910 1
70898 82827...

output:


result: