답안 #837227

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
837227 2023-08-25T08:24:48 Z 1075508020060209tc Love Polygon (BOI18_polygon) C++14
75 / 100
379 ms 33440 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

int n;
vector<int>e[200005];
vector<int>ee[200005];
int dgr[200005];
int alrd[200005];
int lve[200005];
int hs[200005];
void init(){
cin>>n;
int __id=0;
map<string,int>mp;
for(int i=1;i<=n;i++){
    string s;
    string t;
    cin>>s>>t;
    if(mp[s]==0){
        mp[s]=++__id;
    }
    if(mp[t]==0){
        mp[t]=++__id;
    }
    lve[mp[s]]=mp[t];
    e[mp[s]].push_back(mp[t]);
//    ee[mp[s]].push_back(mp[t]);
//    ee[mp[t]].push_back(mp[s]);
    dgr[mp[t]]++;
}
for(int i=1;i<=n;i++){
    if(lve[i]!=i&&lve[lve[i]]==i){
        alrd[i]=1;
        hs[i]=1;
        dgr[i]=0;
    }
}
for(int i=1;i<=n;i++){
    if(lve[i]){continue;}
    if(alrd[e[i][0]]){
        e[i][0]=i;
        dgr[i]++;
    }
}
if(n%2==1){
    cout<<"-1";exit(0);
}
}
int vis[200005];
int dp[200005];
int ans;
void dfs(int nw,int pa){
vis[nw]=1;
for(int i=0;i<ee[nw].size();i++){
    int v=ee[nw][i];
    if(v==pa){continue;}
    dfs(v,nw);
    dp[nw]+=dp[v];
    if(hs[v]==0&&hs[nw]==0){
        hs[v]=1;hs[nw]=1;
        dp[nw]++;
        ans++;
    }
}
}

int uf[200005];
int sz[200005];
int fin(int x){
if(uf[x]==x){return x;}
uf[x]=fin(uf[x]);
return uf[x];
}
void mrg(int a,int b){
int pa=fin(a);int pb=fin(b);
if(pa==pb){return;}
uf[pa]=pb;
sz[pb]+=sz[pa];
}

signed main() {
init();
ans=0;
queue<int>q;
for(int i=1;i<=n;i++){
    if(dgr[i]==0){
        q.push(i);
    }
}
while(q.size()){
    int nw=q.front();
    q.pop();
    for(int i=0;i<e[nw].size();i++){
        int v=e[nw][i];
        dgr[v]--;
        if(dgr[v]==0){
            q.push(v);
        }
        ee[v].push_back(nw);
    }
}
for(int i=1;i<=n;i++){
    if(alrd[i]){continue;}
    if(dgr[i]==1){
        dfs(i,0);
    }
}
for(int i=1;i<=n;i++){
    uf[i]=i;
    sz[i]=1;
}

for(int i=1;i<=n;i++){
    if(hs[i]){continue;}
    if(hs[lve[i]]){continue;}
    mrg(i,lve[i]);
}
for(int i=1;i<=n;i++){
    if(hs[i]){continue;}
    if(fin(i)==i){
        ans+=(sz[fin(i)]+1)/2;
    }
}
cout<<ans<<endl;
}

Compilation message

polygon.cpp: In function 'void dfs(long long int, long long int)':
polygon.cpp:55:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 | for(int i=0;i<ee[nw].size();i++){
      |             ~^~~~~~~~~~~~~~
polygon.cpp: In function 'int main()':
polygon.cpp:94:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     for(int i=0;i<e[nw].size();i++){
      |                 ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 6 ms 9640 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9608 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 4 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 304 ms 24560 KB Output is correct
5 Correct 289 ms 26160 KB Output is correct
6 Correct 290 ms 24472 KB Output is correct
7 Correct 261 ms 22204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 372 ms 26280 KB Output is correct
2 Correct 339 ms 27976 KB Output is correct
3 Correct 250 ms 24492 KB Output is correct
4 Correct 284 ms 22152 KB Output is correct
5 Correct 379 ms 33440 KB Output is correct
6 Correct 323 ms 26652 KB Output is correct
7 Correct 354 ms 26576 KB Output is correct
8 Correct 276 ms 26468 KB Output is correct
9 Correct 261 ms 26872 KB Output is correct
10 Correct 227 ms 25432 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9696 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 6 ms 9640 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9608 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 4 ms 9684 KB Output is correct
16 Correct 6 ms 9684 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 304 ms 24560 KB Output is correct
20 Correct 289 ms 26160 KB Output is correct
21 Correct 290 ms 24472 KB Output is correct
22 Correct 261 ms 22204 KB Output is correct
23 Correct 372 ms 26280 KB Output is correct
24 Correct 339 ms 27976 KB Output is correct
25 Correct 250 ms 24492 KB Output is correct
26 Correct 284 ms 22152 KB Output is correct
27 Correct 379 ms 33440 KB Output is correct
28 Correct 323 ms 26652 KB Output is correct
29 Correct 354 ms 26576 KB Output is correct
30 Correct 276 ms 26468 KB Output is correct
31 Correct 261 ms 26872 KB Output is correct
32 Correct 227 ms 25432 KB Output is correct
33 Correct 4 ms 9684 KB Output is correct
34 Correct 4 ms 9696 KB Output is correct
35 Correct 5 ms 9684 KB Output is correct
36 Correct 5 ms 9736 KB Output is correct
37 Correct 304 ms 26204 KB Output is correct
38 Correct 302 ms 26520 KB Output is correct
39 Incorrect 285 ms 27264 KB Output isn't correct
40 Halted 0 ms 0 KB -