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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <bits/stdc++.h>
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define TRAV(i, n) for(auto &i : n)
#define SZ(x) (int)(x).size()
#define PR std::pair
#define MP std::make_pair
#define X first
#define Y second
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

const int MOD = 1000000007;
int n, m, A, B;
std::vector<VI> g, rev, scc;
std::vector<VI> mam;

VI ord;
std::vector<bool> vis;
VI ktora;

void dfs(int v){
	vis[v] = true;
	TRAV(ch, rev[v]){
		if(!vis[ch]) dfs(ch);
	}
	ord.push_back(v);
}

void dfsrev(int v){
	vis[v] = true;
	ktora[v] = SZ(scc)-1;
	if(v < A) mam.back().push_back(v);
	TRAV(ch, g[v]){
		if(!vis[ch]) dfsrev(ch);
		else if(ktora[ch] != ktora[v]){
			scc[ktora[ch]].push_back(ktora[v]);
		}
	}
}

std::vector<PII> prze;

struct Tree{
	VI t, u;
	int BOTTOM;
	void build(int sz){
		BOTTOM = 1;
		while(BOTTOM < sz) BOTTOM *= 2;
		t.resize(BOTTOM*2);
		u.resize(BOTTOM*2);
	}
	void upd(int v){
		t[v] += u[v];
		if(v < BOTTOM) u[v<<1] += u[v], u[(v<<1)+1] += u[v];
		u[v] = 0;
	}
	int query(int a, int b, int v=1, int lo=0, int hi=-2){
		if(hi==-2) hi = BOTTOM-1;
		upd(v);
		if(a>b) return 1;
		if(a==lo&&b==hi){
			return t[v];
		}
		int mid = (lo+hi)/2;
		return std::min(query(a, std::min(b, mid), v<<1, lo, mid),
						query(std::max(a, mid+1), b, (v<<1)+1, mid+1, hi));
	}
	void add(int a, int b, int w, int v=1, int lo=0, int hi=-2){
		if(hi==-2) hi = BOTTOM-1;
		upd(v);
		if(a>b) return;
		if(a==lo&&b==hi){
			u[v] += w;
			upd(v);
			return;
		}
		int mid = (lo+hi)/2;
		add(a, std::min(b, mid), w, v<<1, lo, mid);
		add(std::max(a, mid+1), b, w, (v<<1)+1, mid+1, hi);
		t[v] = std::min(t[v<<1], t[(v<<1)+1]);
	}
};

int main(){
	std::scanf("%d%d%d%d", &n, &m, &A, &B);
	g.resize(n);
	rev.resize(n);
	FOR(i, m){
		int a;
		char c1, c2;
		int b;
		std::scanf("%d %c%c %d", &a, &c1, &c2, &b);
		a--;b--;
		g[a].push_back(b);
		rev[b].push_back(a);
		if(c2 == '-') g[b].push_back(a), rev[a].push_back(b);
	}
	vis.resize(n);
	FOR(i, n) if(!vis[i]) dfs(i);
	std::fill(vis.begin(), vis.end(), false);
	std::reverse(ord.begin(), ord.end());
	ktora.resize(n);
	TRAV(i, ord) if(!vis[i]){
		scc.emplace_back();
		mam.emplace_back();
		dfsrev(i);
	}
/*
	FOR(i, n){
		std::cout << i+1 << " spojna " << ktora[i]+1 << std::endl;
	}
	FOR(i, SZ(scc)){
		TRAV(ch, scc[i]){
			std::cout << ch+1 << " -> " << i+1 << std::endl;
		}
	}
*/

	prze.resize(SZ(scc), MP(-1, -1));
	for(int i = SZ(scc)-1; i >= 0; --i){
		std::vector<PII> p;
		TRAV(ch, mam[i]) p.push_back(MP(ch, 1)), p.push_back(MP(ch+1, -1));
		TRAV(ch, scc[i]){
			if(prze[ch].X != -1){
				if(prze[ch].X > prze[ch].Y){
					p.push_back(MP(prze[ch].X, 1));
					p.push_back(MP(A, -1));
					p.push_back(MP(0, 1));
					p.push_back(MP(prze[ch].Y+1, -1));
				}else{
					p.push_back(MP(prze[ch].X, 1));
					p.push_back(MP(prze[ch].Y+1, -1));
				}
			}
		}
/*		std::cout << "spojna " << i+1 << " eventy ";
		TRAV(ev, p){
			std::cout << ev.X+1 << "," << ev.Y << "   ";
		}
		std::cout << std::endl;
		*/
		if(p.empty()) continue;
		std::sort(p.begin(), p.end());
		std::vector<PII> xd;
		int otw = 0;
		int start = -1;
		auto it = p.begin();
		while(it != p.end()){
			int nowx = it->X;
			while(it != p.end() && it->X == nowx){
				otw += it->Y;
				it++;
			}
			if(otw > 0){
				if(start == -1) start = nowx;
			}
			if(otw == 0){
				xd.push_back(MP(start, nowx-1));
				start = -1;
			}
		}
		assert(SZ(xd) <= 2);
		assert(SZ(xd) >= 1);
		if(SZ(xd) == 2){
			assert(xd[0].X == 0 && xd[1].Y == A-1);
			assert(xd[1].X > xd[0].Y);
			prze[i] = MP(xd[1].X, xd[0].Y);
		}else{
			assert(xd[0].X <= xd[0].Y);
			prze[i] = xd[0];
		}
	//	std::cout << "prze " << i+1 << " = " << prze[i].X+1 << " - " << prze[i].Y+1 << std::endl;
	}
//	REP(i, A, A+B) std::cout << prze[ktora[i]].X+1 << " " << prze[ktora[i]].Y+1 << "\n";
	Tree tree;
	tree.build(A);
	int ans = 0;
	FOR(mask, 1<<B){
		FOR(i, B) if(mask & (1<<i)){
			int ind = A+i;
			if(prze[ktora[ind]].X != -1){
				if(prze[ktora[ind]].X <= prze[ktora[ind]].Y){
					tree.add(prze[ktora[ind]].X, prze[ktora[ind]].Y, 1);
				}else{
					tree.add(0, prze[ktora[ind]].Y, 1);
					tree.add(prze[ktora[ind]].X, A-1, 1);

				}
			}
		}
		if(tree.query(0, A-1) > 0){
			ans++;
		}
		FOR(i, B) if(mask & (1<<i)){
			int ind = A+i;
			if(prze[ktora[ind]].X != -1){
				if(prze[ktora[ind]].X <= prze[ktora[ind]].Y){
					tree.add(prze[ktora[ind]].X, prze[ktora[ind]].Y, -1);
				}else{
					tree.add(0, prze[ktora[ind]].Y, -1);
					tree.add(prze[ktora[ind]].X, A-1, -1);
				}
			}
		}
	}
	std::printf("%d\n", ans);
	return 0;
}