답안 #951576

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
951576 2024-03-22T06:59:37 Z huangallen Love Polygon (BOI18_polygon) C++17
100 / 100
92 ms 26092 KB
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#define int long long
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define RREP(i,n) for(int i=(n)-1;i>=0;i--)
#define RREP1(i,n) for(int i=(n);i>=1;i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int,int>
#define pipii pair<int,pii>
#define Graph vector<vector<int>>
#define IOS() ios::sync_with_stdio(0),cin.tie(0)
#define md(x) (((x)%(mod)+(mod))%(mod))
#define MD(x,M) (((x)%(M)+(M))%(M))
#define ld long double
#define pdd pair<ld,ld>
#ifdef LOCAL
#define op(x) cout<<(#x)<<"="<<(x)<<", ";
#define ope(x) cout<<(#x)<<"="<<(x)<<endl;
#define oparr(x) cout<<(#x)<<":";REP(allen,(x).size()) cout<<x[allen]<<" ";cout<<" size="<<(x).size()<<endl;
#define entr cout<<endl;
#else
#define op(x) ;
#define ope(x) ;
#define oparr(x) ;
#define entr ;
#endif
const int mod=1e9+7;
const int maxn=255;
const int maxv=1e5+5;
const int inf=(1ll<<62);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rd(int l,int r) {
    return uniform_int_distribution<int>(l,r)(rng);
}
//shuffle(a,a+n,rng)
//
//#include<bits/extc++.h>
//using namespace __gnu_pbds;
//template<typename T> using pb_ds_tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//
//using Tree = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
struct chash {
    int operator()(string s)const {
        int r=0;
        for(char c:s) r=(r<<4)+(r<<3)+(r<<1)+r+c-'a'+1;
        return r;
    }
};
unordered_map<string,int,chash> ump;
int n;
vector<int> to,ind;
vector<bool> inc,vis;
Graph gb;
int an=0;
pii dfs(int u) {//no,yes
    vis[u]=1;
    pii an={0,0};
    int mxs=0;
    for(int v:gb[u]) {
        if(vis[v]) continue;
        pii ret=dfs(v);
        an.f+=ret.s;
        mxs=max(mxs,ret.f-ret.s+1);
    }
    an.s=an.f+mxs;
    return an;
}
void vis_to_0(int u) {
    vis[u]=0;
    for(int v:gb[u]) {
        if(vis[v]) vis_to_0(v);
    }
}
void cal_j(int u) {
    int t=to[u],cnt=1,ret=0;
    while(t!=u) {
        cnt++;
        t=to[t];
    }
    if(cnt==1) ret+=dfs(u).s;
    else if(cnt==2) {
        ret=dfs(u).s;
        vis_to_0(u);
        vis[u]=1,vis[to[u]]=1;
        ret=max(ret,dfs(u).f+dfs(to[u]).f+2);
    }
    else {
        ret=dfs(u).s;
        vis_to_0(u);
        vis[u]=1;
        ret=max(ret,1+dfs(to[u]).f+dfs(u).f);
    }
    an+=ret;
}
signed main() {
    IOS();
    cin>>n;
    if(n&1) {
        cout<<"-1\n";
        return 0;
    }
    gb=Graph(n);
    to=vector<int>(n);
    ind=vector<int>(n);
    vector<string> in_s(n);
    inc=vector<bool>(n,1);
    vis=vector<bool>(n,0);
    REP(i,n) {
        string u;
        cin>>u>>in_s[i];
        ump[u]=i;
    }
    REP(i,n) {
        to[i]=ump[in_s[i]];
        ind[to[i]]++;
        gb[to[i]].pb(i);
    }
    queue<int> q;
    REP(i,n) if(ind[i]==0) q.push(i);
    while(q.size()) {
        int u=q.front();
        q.pop();
        inc[u]=0;
        ind[to[u]]--;
        if(ind[to[u]]==0) q.push(to[u]);
    }
//    oparr(inc)oparr(ind)
    REP(i,n) {
        if(inc[i]&&!vis[i]) cal_j(i);
    }
    an=n-an;
    cout<<an<<'\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 81 ms 25016 KB Output is correct
5 Correct 61 ms 18120 KB Output is correct
6 Correct 80 ms 26092 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 17612 KB Output is correct
2 Correct 68 ms 19444 KB Output is correct
3 Correct 49 ms 18128 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 92 ms 25528 KB Output is correct
6 Correct 62 ms 17348 KB Output is correct
7 Correct 61 ms 17248 KB Output is correct
8 Correct 59 ms 17348 KB Output is correct
9 Correct 56 ms 17092 KB Output is correct
10 Correct 40 ms 16732 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 81 ms 25016 KB Output is correct
20 Correct 61 ms 18120 KB Output is correct
21 Correct 80 ms 26092 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 69 ms 17612 KB Output is correct
24 Correct 68 ms 19444 KB Output is correct
25 Correct 49 ms 18128 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 92 ms 25528 KB Output is correct
28 Correct 62 ms 17348 KB Output is correct
29 Correct 61 ms 17248 KB Output is correct
30 Correct 59 ms 17348 KB Output is correct
31 Correct 56 ms 17092 KB Output is correct
32 Correct 40 ms 16732 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 81 ms 18064 KB Output is correct
38 Correct 68 ms 18580 KB Output is correct
39 Correct 65 ms 17348 KB Output is correct
40 Correct 66 ms 17220 KB Output is correct
41 Correct 65 ms 17352 KB Output is correct
42 Correct 82 ms 17524 KB Output is correct
43 Correct 79 ms 17504 KB Output is correct
44 Correct 83 ms 17532 KB Output is correct
45 Correct 72 ms 17604 KB Output is correct
46 Correct 72 ms 17608 KB Output is correct
47 Correct 59 ms 17348 KB Output is correct
48 Correct 62 ms 17700 KB Output is correct
49 Correct 83 ms 19652 KB Output is correct
50 Correct 50 ms 18120 KB Output is correct
51 Correct 0 ms 344 KB Output is correct
52 Correct 72 ms 25380 KB Output is correct
53 Correct 64 ms 17328 KB Output is correct
54 Correct 79 ms 17396 KB Output is correct
55 Correct 56 ms 17352 KB Output is correct
56 Correct 55 ms 17096 KB Output is correct
57 Correct 40 ms 16796 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 344 KB Output is correct
61 Correct 1 ms 344 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 87 ms 24852 KB Output is correct
66 Correct 62 ms 18244 KB Output is correct
67 Correct 81 ms 26056 KB Output is correct
68 Correct 0 ms 344 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 0 ms 344 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 600 KB Output is correct
79 Correct 0 ms 344 KB Output is correct
80 Correct 0 ms 344 KB Output is correct
81 Correct 0 ms 596 KB Output is correct
82 Correct 0 ms 344 KB Output is correct
83 Correct 1 ms 348 KB Output is correct