-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgauss.cpp
157 lines (148 loc) · 4.15 KB
/
gauss.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#define _CRT_SECURE_NO_DEPRECATE
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define int int64_t
#define SYNC std::ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
//typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update>
// ost;
#define rep(i,l,r) for (int i = (l); i < (r); i++)
#define here cout << " Hey!!\n";
#define pb push_back
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define sz(a) (int)((a).size())
#define sq(x) ((x)*(x))
template <typename T>
void etch(T V) {
for (auto x : V) cout << x << ' ' ;
cout << '\n';
}
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while(!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
stringstream ss; ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
const int MOD = 2;
const int MOD1 = 998244353;
const int N = 2e5+5;
const ld PI = 3.141592653589793116;
int modular_exp(int base,int exp,int mod)
{
int ans =1;
while(exp)
{
if(exp%2) ans = ans%mod*base%mod;
base = (base%mod*base%mod)%mod;
exp/=2;
ans%=mod;
}
return ans;
}
int inv_mod(int base,int mod)
{
return modular_exp(base,mod-2,mod);
}
const double EPS = 1e-9;
const int INF = 2; // it doesn't actually have to be infinity or a big number
int gauss (vector < vector<int> > a, vector<int> & ans) {
int n = (int) a.size();
int m = (int) a[0].size() - 1;
vector<int> where (m, -1);
for (int col=0, row=0; col<m && row<n; ++col) {
int sel = row;
for (int i=row; i<n; ++i)
if (abs (a[i][col]) > abs (a[sel][col]))
sel = i;
if (abs (a[sel][col]) < EPS)
continue;
for (int i=col; i<=m; ++i)
swap (a[sel][i], a[row][i]);
where[col] = row;
for (int i=0; i<n; ++i)
if (i != row) {
int c = (a[i][col] * inv_mod(a[row][col], MOD))%MOD;
for (int j=col; j<=m; ++j) {
a[i][j] -= a[row][j] * c;
a[i][j] %= MOD;
if (a[i][j] < 0) a[i][j] += MOD;
}
}
++row;
}
ans.assign (m, 0);
for (int i=0; i<m; ++i)
if (where[i] != -1)
ans[i] = (a[where[i]][m] * inv_mod(a[where[i]][i], MOD))%MOD;
for (int i=0; i<n; ++i) {
int sum = 0;
for (int j=0; j<m; ++j) {
sum += ans[j] * a[i][j];
sum %= MOD;
if (sum < 0) sum += MOD;
}
if (abs (sum - a[i][m]) > EPS)
return 0;
}
for (int i=0; i<m; ++i)
if (where[i] == -1)
return INF;
return 1;
}
int32_t main()
{
SYNC
int n, m; cin >> n >> m;
vi g[n];
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
u--, v--;
g[u].pb(v);
g[v].pb(u);
}
vector<vi> A(n, vi(n, 0));
vi B(n, 1), X;
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int v : g[i]) {
A[i][v] = 1;
cnt++;
}
A[i][i] = cnt & 1LL;
}
rep(i,0,n) A[i].pb(B[i]);
cout << (gauss(A, X) > 0 ? "Y" : "N");
return 0;
}