#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5;
int n, t, vs[nx], res, cnt, l[nx], c[nx], used[nx];
string su[nx], sv[nx];
map<string, int> mp;
vector<int> d[nx], cy[nx];
int dfs(int u, int rt)
{
vs[u]=1;
for (auto v:d[u]) if (v==rt||(!vs[v]&&dfs(v, rt))) return c[u]=cnt, cy[cnt].push_back(u), 1;
return 0;
}
void dfstree(int u, int p)
{
for (auto v:d[u]) if (c[v]==-1) dfstree(v, u);
if (u!=p&&!used[u]&&!used[p]) res++, used[u]=used[p]=1;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>su[i]>>sv[i], mp[su[i]]=i, c[i]=-1;
if (n%2) return cout<<-1, 0;
for (int i=1; i<=n; i++)
{
l[mp[su[i]]]=mp[sv[i]];
if (mp[su[i]]!=mp[sv[i]]) d[mp[sv[i]]].push_back(mp[su[i]]);
else cy[cnt++].push_back(i);
}
for (int i=1; i<=n; i++) if (!vs[i]&&dfs(i, i)) cnt++;
for (int i=0; i<cnt; i++)
{
if (cy[i].size()==2) res+=2, used[cy[i][0]]=used[cy[i][1]]=1;
int cnt=0;
for (auto x:cy[i]) dfstree(x, x), cnt+=(!used[x]);
if (cnt==cy[i].size())
{
if (cy[i].size()==2) res+=2;
else res+=cy[i].size()/2;
}
for (auto x:cy[i])
{
if (used[x]&&!used[l[x]])
{
int tmp=l[x], sz=1;
while (!used[l[tmp]]) sz++, tmp=l[tmp];
res+=sz/2;
}
}
}
cout<<n-res;
}
Compilation message
polygon.cpp: In function 'int main()':
polygon.cpp:43:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if (cnt==cy[i].size())
| ~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12908 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12944 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12940 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
199 ms |
32044 KB |
Output is correct |
5 |
Correct |
177 ms |
26704 KB |
Output is correct |
6 |
Correct |
168 ms |
32980 KB |
Output is correct |
7 |
Correct |
53 ms |
22864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
25172 KB |
Output is correct |
2 |
Correct |
169 ms |
26964 KB |
Output is correct |
3 |
Correct |
132 ms |
25940 KB |
Output is correct |
4 |
Correct |
50 ms |
22864 KB |
Output is correct |
5 |
Correct |
181 ms |
31760 KB |
Output is correct |
6 |
Correct |
169 ms |
24400 KB |
Output is correct |
7 |
Correct |
166 ms |
24516 KB |
Output is correct |
8 |
Correct |
162 ms |
25680 KB |
Output is correct |
9 |
Correct |
165 ms |
23996 KB |
Output is correct |
10 |
Correct |
120 ms |
23504 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12908 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12944 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12940 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
199 ms |
32044 KB |
Output is correct |
20 |
Correct |
177 ms |
26704 KB |
Output is correct |
21 |
Correct |
168 ms |
32980 KB |
Output is correct |
22 |
Correct |
53 ms |
22864 KB |
Output is correct |
23 |
Correct |
167 ms |
25172 KB |
Output is correct |
24 |
Correct |
169 ms |
26964 KB |
Output is correct |
25 |
Correct |
132 ms |
25940 KB |
Output is correct |
26 |
Correct |
50 ms |
22864 KB |
Output is correct |
27 |
Correct |
181 ms |
31760 KB |
Output is correct |
28 |
Correct |
169 ms |
24400 KB |
Output is correct |
29 |
Correct |
166 ms |
24516 KB |
Output is correct |
30 |
Correct |
162 ms |
25680 KB |
Output is correct |
31 |
Correct |
165 ms |
23996 KB |
Output is correct |
32 |
Correct |
120 ms |
23504 KB |
Output is correct |
33 |
Correct |
2 ms |
12892 KB |
Output is correct |
34 |
Correct |
2 ms |
12892 KB |
Output is correct |
35 |
Correct |
2 ms |
12892 KB |
Output is correct |
36 |
Correct |
2 ms |
12888 KB |
Output is correct |
37 |
Correct |
204 ms |
25732 KB |
Output is correct |
38 |
Correct |
181 ms |
25940 KB |
Output is correct |
39 |
Correct |
177 ms |
24848 KB |
Output is correct |
40 |
Correct |
169 ms |
24404 KB |
Output is correct |
41 |
Correct |
162 ms |
24492 KB |
Output is correct |
42 |
Correct |
184 ms |
25012 KB |
Output is correct |
43 |
Correct |
173 ms |
24872 KB |
Output is correct |
44 |
Correct |
168 ms |
24912 KB |
Output is correct |
45 |
Correct |
168 ms |
24916 KB |
Output is correct |
46 |
Correct |
160 ms |
24968 KB |
Output is correct |
47 |
Correct |
159 ms |
23988 KB |
Output is correct |
48 |
Correct |
179 ms |
25172 KB |
Output is correct |
49 |
Correct |
170 ms |
26964 KB |
Output is correct |
50 |
Correct |
120 ms |
25944 KB |
Output is correct |
51 |
Correct |
53 ms |
22868 KB |
Output is correct |
52 |
Correct |
183 ms |
31828 KB |
Output is correct |
53 |
Correct |
166 ms |
24340 KB |
Output is correct |
54 |
Correct |
169 ms |
24404 KB |
Output is correct |
55 |
Correct |
153 ms |
25684 KB |
Output is correct |
56 |
Correct |
177 ms |
23892 KB |
Output is correct |
57 |
Correct |
124 ms |
23360 KB |
Output is correct |
58 |
Correct |
2 ms |
12892 KB |
Output is correct |
59 |
Correct |
2 ms |
12892 KB |
Output is correct |
60 |
Correct |
2 ms |
12892 KB |
Output is correct |
61 |
Correct |
2 ms |
12892 KB |
Output is correct |
62 |
Correct |
2 ms |
12708 KB |
Output is correct |
63 |
Correct |
2 ms |
12892 KB |
Output is correct |
64 |
Correct |
2 ms |
12892 KB |
Output is correct |
65 |
Correct |
205 ms |
32020 KB |
Output is correct |
66 |
Correct |
183 ms |
26708 KB |
Output is correct |
67 |
Correct |
193 ms |
32984 KB |
Output is correct |
68 |
Correct |
52 ms |
23120 KB |
Output is correct |
69 |
Correct |
2 ms |
12892 KB |
Output is correct |
70 |
Correct |
2 ms |
12892 KB |
Output is correct |
71 |
Correct |
2 ms |
12892 KB |
Output is correct |
72 |
Correct |
2 ms |
12892 KB |
Output is correct |
73 |
Correct |
2 ms |
12804 KB |
Output is correct |
74 |
Correct |
2 ms |
12892 KB |
Output is correct |
75 |
Correct |
2 ms |
12892 KB |
Output is correct |
76 |
Correct |
2 ms |
12944 KB |
Output is correct |
77 |
Correct |
2 ms |
12892 KB |
Output is correct |
78 |
Correct |
2 ms |
12940 KB |
Output is correct |
79 |
Correct |
2 ms |
12892 KB |
Output is correct |
80 |
Correct |
2 ms |
12892 KB |
Output is correct |
81 |
Correct |
2 ms |
12892 KB |
Output is correct |
82 |
Correct |
2 ms |
12892 KB |
Output is correct |
83 |
Correct |
2 ms |
12892 KB |
Output is correct |