답안 #376949

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376949 2021-03-12T09:55:41 Z AriaH Love Polygon (BOI18_polygon) C++11
29 / 100
383 ms 43884 KB
/** I can do this all day **/

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;

typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define SZ(x)			    		(int)x.size()
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);

const int N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;

ll pw(ll a , ll b, ll M)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }

char C[N];

string ask()
{
	scanf("%s", C);
	string s = C;
	return s;
}

int n, mark[N], dp[2][N]; /// dp[0][v] -> v match shode bashe
						  /// dp[1][v] -> v match nashode bashe

map < string , int > mp;

vector < int > G[N], roots;

void dfs(int v)
{
	mark[v] = 1;
	for(auto u : G[v])
	{
		if(mark[u]) continue;
		dfs(u);
		dp[0][v] = max(dp[0][v] + max(dp[0][u], dp[1][u]), dp[1][v] + dp[1][u] + 1);
		dp[1][v] = dp[1][v] + max(dp[0][u], dp[1][u]);
	}
}

int main()
{
	scanf("%d", &n);
	if(n & 1)
	{
		return !printf("-1");
	}
	int cnt = 0;
	for(int i = 1; i <= n; i ++)
	{
		string s = ask(), t = ask();
		if(mp.count(s) == 0) mp[s] = ++ cnt;
		if(mp.count(t) == 0) mp[t] = ++ cnt;
		if(mp[s] == mp[t]) { roots.push_back(mp[s]); continue; }
		G[mp[t]].push_back(mp[s]);
	}
	int tot = n;
	for(auto i : roots)
	{
		if(mark[i]) continue;
		dfs(i);
		tot -= max(dp[0][i], dp[1][i]);
	}
	printf("%d\n", tot);
	return 0;
}

/** test corner cases(n = 1?) watch for overflow or minus indices **/

Compilation message

polygon.cpp: In function 'std::string ask()':
polygon.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   31 |  scanf("%s", C);
      |  ~~~~~^~~~~~~~~
polygon.cpp: In function 'int main()':
polygon.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   57 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 15 ms 23788 KB Output is correct
4 Incorrect 16 ms 23788 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Incorrect 16 ms 23788 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 383 ms 37356 KB Output is correct
2 Correct 371 ms 39404 KB Output is correct
3 Correct 293 ms 34532 KB Output is correct
4 Correct 15 ms 23788 KB Output is correct
5 Correct 374 ms 43884 KB Output is correct
6 Correct 382 ms 36588 KB Output is correct
7 Correct 356 ms 36608 KB Output is correct
8 Correct 313 ms 36328 KB Output is correct
9 Correct 339 ms 36076 KB Output is correct
10 Correct 252 ms 34660 KB Output is correct
11 Correct 16 ms 23788 KB Output is correct
12 Correct 15 ms 23788 KB Output is correct
13 Correct 15 ms 23916 KB Output is correct
14 Correct 15 ms 23788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Correct 15 ms 23788 KB Output is correct
4 Incorrect 16 ms 23788 KB Output isn't correct
5 Halted 0 ms 0 KB -