Submission #642698

# Submission time Handle Problem Language Result Execution time Memory
642698 2022-09-20T11:51:35 Z andreast12 Paths (BOI18_paths) C++17
100 / 100
423 ms 24820 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
const int mod=1e9+7, maxn=3e5+5;
int n, m, k, c[maxn], u, v, tmp, order[125][10], p[10], NEXT[10], now;
vector<int> adj[maxn];
bool vis[10];
ll byk[maxn], tot[10], ans;
queue<int> q;
string s;
map<string, bool> pernah;

void perm(int idx) {
	if(idx>k) {
		tmp++;
		for(int i=1; i<=k; i++) order[tmp][i]=p[i];
		return;
	}
	for(int i=1; i<=k; i++) {
		if(!vis[i]) {
			vis[i]=true;
			p[idx]=i;
			perm(idx+1);
			vis[i]=false;
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> m >> k;
	for(int i=1; i<=n; i++) cin >> c[i];
	while(m--) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	perm(1);
	for(int i=1; i<=tmp; i++) {
		for(int j=1; j<=k; j++) NEXT[order[i][j]]=order[i][j+1], tot[j]=0; 
		for(int j=1; j<=n; j++) {
			if(c[j]==order[i][1]) {
				q.push(j);
				byk[j]=1;
			} else byk[j]=-1;
		}
		while(!q.empty()) {
			now=q.front();
			tot[c[now]]+=byk[now];
			q.pop();
			for(auto nxt:adj[now]) {
				if(c[nxt]==NEXT[c[now]]) {
					if(byk[nxt]==-1) {
						byk[nxt]=byk[now];
						q.push(nxt);
					} else byk[nxt]+=byk[now];
				}		
			}
		}
		s=order[i][1]-'0';
		for(int j=2; j<=k; j++) {
			s+=order[i][j]-'0';
			if(!pernah[s]) {
				ans+=tot[order[i][j]];
				pernah[s]=true;
			}
		}
	}
	cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7372 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 4 ms 7388 KB Output is correct
7 Correct 5 ms 7376 KB Output is correct
8 Correct 5 ms 7376 KB Output is correct
9 Correct 4 ms 7384 KB Output is correct
10 Correct 5 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 11636 KB Output is correct
2 Correct 64 ms 11628 KB Output is correct
3 Correct 276 ms 24140 KB Output is correct
4 Correct 89 ms 15556 KB Output is correct
5 Correct 76 ms 15564 KB Output is correct
6 Correct 191 ms 21420 KB Output is correct
7 Correct 298 ms 24140 KB Output is correct
8 Correct 254 ms 24820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7372 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7372 KB Output is correct
6 Correct 4 ms 7388 KB Output is correct
7 Correct 5 ms 7376 KB Output is correct
8 Correct 5 ms 7376 KB Output is correct
9 Correct 4 ms 7384 KB Output is correct
10 Correct 5 ms 7380 KB Output is correct
11 Correct 71 ms 11636 KB Output is correct
12 Correct 64 ms 11628 KB Output is correct
13 Correct 276 ms 24140 KB Output is correct
14 Correct 89 ms 15556 KB Output is correct
15 Correct 76 ms 15564 KB Output is correct
16 Correct 191 ms 21420 KB Output is correct
17 Correct 298 ms 24140 KB Output is correct
18 Correct 254 ms 24820 KB Output is correct
19 Correct 78 ms 13800 KB Output is correct
20 Correct 72 ms 13260 KB Output is correct
21 Correct 242 ms 24172 KB Output is correct
22 Correct 89 ms 15576 KB Output is correct
23 Correct 91 ms 15672 KB Output is correct
24 Correct 182 ms 21280 KB Output is correct
25 Correct 247 ms 24140 KB Output is correct
26 Correct 288 ms 24820 KB Output is correct
27 Correct 105 ms 13208 KB Output is correct
28 Correct 123 ms 14740 KB Output is correct
29 Correct 393 ms 24012 KB Output is correct
30 Correct 331 ms 19284 KB Output is correct
31 Correct 346 ms 19192 KB Output is correct
32 Correct 423 ms 24048 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 5 ms 7380 KB Output is correct
35 Correct 4 ms 7380 KB Output is correct
36 Correct 4 ms 7380 KB Output is correct
37 Correct 4 ms 7360 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 4 ms 7380 KB Output is correct
40 Correct 4 ms 7376 KB Output is correct
41 Correct 4 ms 7384 KB Output is correct
42 Correct 3 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 105 ms 9140 KB Output is correct
3 Correct 23 ms 9220 KB Output is correct
4 Correct 56 ms 12820 KB Output is correct
5 Correct 52 ms 13600 KB Output is correct
6 Correct 219 ms 12704 KB Output is correct
7 Correct 34 ms 9216 KB Output is correct
8 Correct 92 ms 12820 KB Output is correct
9 Correct 79 ms 13556 KB Output is correct
10 Correct 81 ms 13348 KB Output is correct
11 Correct 160 ms 11004 KB Output is correct
12 Correct 154 ms 12556 KB Output is correct
13 Correct 157 ms 11100 KB Output is correct
14 Correct 215 ms 12784 KB Output is correct
15 Correct 238 ms 12876 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 4 ms 7376 KB Output is correct
18 Correct 8 ms 7376 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 4 ms 7380 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 4 ms 7376 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct