답안 #598483

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
598483 2022-07-18T11:53:00 Z urosk Paths (BOI18_paths) C++14
100 / 100
704 ms 47484 KB
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);

using namespace std;
#define maxn 300005
#define maxk 6
ll n,m,k;
ll ans = 0;
vector<ll> g[maxn];
ll a[maxn];
ll dp[maxn][maxk][maxk];
ll cnt[maxn][maxk];
vector<pll> edg;
void kraj(){
    cout<<ans<<endl;
    exit(0);
}
pll fale(vector<ll> v){
    vector<bool> tu(k+1);
    for(ll x : v) tu[x] = 1;
    pll p = {-1,-1};
    for(ll i = 1;i<=k;i++){
        if(!tu[i]){
            if(p.fi==-1) p.fi = i;
            else p.sc = i;
        }
    }
    return p;
}
int main(){
	daj_mi_malo_vremena
    cin >> n >> m >> k;
    for(ll i = 1;i<=n;i++) cin >> a[i];
    for(ll i = 1;i<=m;i++){
        ll x,y; cin >> x >> y;
        edg.pb({x,y});
        g[x].pb(y);
        g[y].pb(x);
    }
    if(k==1) kraj();
    ll cur = 0;
    for(ll i = 1;i<=n;i++){
        for(ll j : g[i]){
            if(a[i]==a[j]) continue;
            cnt[i][a[j]]++;
            cur++;
        }
    }
    ans+=cur;
    if(k==2) kraj();
    cur = 0;
    for(ll i = 1;i<=n;i++){
        vector<ll> v(k+1);
        for(ll j : g[i]){
            v[a[j]]++;
        }
        ll coli = a[i];
        for(ll e = 1;e<=k;e++){
            for(ll r = e+1;r<=k;r++){
                if(e==coli||r==coli) continue;
                cur+=v[e]*v[r];
            }
        }
    }
    ans+=2*cur;
    cerr<<"k = 3: "<<ans<<endl;
    if(k==3) kraj();
    cur = 0;
    for(pll p : edg){
        ll x = p.fi;
        ll y = p.sc;
        ll colx = a[x];
        ll coly = a[y];
        if(colx==coly) continue;
        for(ll e = 1;e<=k;e++){
            if(e==colx||e==coly) continue;
            for(ll r = e+1;r<=k;r++){
                if(r==colx||r==coly) continue;
                cur+=cnt[x][e]*cnt[y][r];
                cur+=cnt[x][r]*cnt[y][e];
            }
        }
    }
    cur*=2;
    ans+=cur;
    if(k==4) kraj();
    for(ll i = 1;i<=n;i++){
        for(ll j : g[i]){
            ll coli = a[i];
            ll colj = a[j];
            for(ll e = 1;e<=k;e++){
                dp[i][colj][e] += cnt[j][e];
                if(e==coli) dp[i][colj][e]--;
            }
        }
    }
    cur = 0;
    for(ll i = 1;i<=n;i++){
        ll coli = a[i];
        for(ll e = 1;e<=k;e++){
            for(ll r = 1;r<=k;r++){
                pll p = fale({coli,e,r});
                ll f = p.fi;
                ll g = p.sc;
                set<ll> s;
                s.insert(coli);
                s.insert(r);
                s.insert(e);
                s.insert(g);
                s.insert(f);
                if(sz(s)!=5) continue;
                cur+=dp[i][e][r]*dp[i][f][g];
                cur+=dp[i][e][r]*dp[i][g][f];
            }
        }
    }
    ans+=cur;
    if(k==5) kraj();
	return 0;
}
/*
4 3 3
1 2 1 3
1 2
2 3
4 2

9 11 4
1 2 3 4 1 2 1 2 2
1 2
1 3
2 3
2 4
3 6
6 2
6 5
4 3
4 5
7 8
9 8
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7396 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 21368 KB Output is correct
2 Correct 61 ms 22668 KB Output is correct
3 Correct 267 ms 39036 KB Output is correct
4 Correct 98 ms 23216 KB Output is correct
5 Correct 121 ms 23196 KB Output is correct
6 Correct 181 ms 31876 KB Output is correct
7 Correct 286 ms 39032 KB Output is correct
8 Correct 281 ms 39520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7396 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 82 ms 21368 KB Output is correct
12 Correct 61 ms 22668 KB Output is correct
13 Correct 267 ms 39036 KB Output is correct
14 Correct 98 ms 23216 KB Output is correct
15 Correct 121 ms 23196 KB Output is correct
16 Correct 181 ms 31876 KB Output is correct
17 Correct 286 ms 39032 KB Output is correct
18 Correct 281 ms 39520 KB Output is correct
19 Correct 70 ms 21296 KB Output is correct
20 Correct 61 ms 22632 KB Output is correct
21 Correct 274 ms 39008 KB Output is correct
22 Correct 95 ms 23304 KB Output is correct
23 Correct 88 ms 23160 KB Output is correct
24 Correct 185 ms 31868 KB Output is correct
25 Correct 282 ms 39128 KB Output is correct
26 Correct 317 ms 39496 KB Output is correct
27 Correct 63 ms 22624 KB Output is correct
28 Correct 84 ms 22860 KB Output is correct
29 Correct 312 ms 38952 KB Output is correct
30 Correct 185 ms 29092 KB Output is correct
31 Correct 194 ms 29140 KB Output is correct
32 Correct 294 ms 38956 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 4 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 7292 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 7380 KB Output is correct
41 Correct 4 ms 7252 KB Output is correct
42 Correct 4 ms 7252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 29 ms 12184 KB Output is correct
3 Correct 21 ms 12104 KB Output is correct
4 Correct 62 ms 19244 KB Output is correct
5 Correct 54 ms 19616 KB Output is correct
6 Correct 704 ms 47484 KB Output is correct
7 Correct 25 ms 12232 KB Output is correct
8 Correct 76 ms 19272 KB Output is correct
9 Correct 58 ms 19684 KB Output is correct
10 Correct 58 ms 19884 KB Output is correct
11 Correct 365 ms 29344 KB Output is correct
12 Correct 505 ms 38612 KB Output is correct
13 Correct 357 ms 30264 KB Output is correct
14 Correct 695 ms 47420 KB Output is correct
15 Correct 689 ms 47472 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 5 ms 7368 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 7368 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct