Once upon a time, there arose a huge discussion among the dwarves in Dwarfland. The…
- User
Once upon a time, there arose a huge discussion among the dwarves in Dwarfland. The government wanted to introduce an identity card for all inhabitants. Most dwarves accept to be small, but they do not like to be measured. Therefore, the government allowed them to substitute the field "height" in their personal identity card with a field "relative dwarf size". For producing the ID cards, the dwarves were being interviewed about their relative sizes. For some reason, the government suspects that at least one of the interviewed dwarves must have lied. Can you help find out if the provided information proves the existence of at least one lying dwarf? Input The first line contains the number n
( 1 ≤ n ≤ 1 0 5 ) n (1≤n≤10 5 ) of statements. The next n n lines describing the relations between the dwarves. Each relation is described by one line with " s 1 < s 2 s 1 <s 2 " or " s 1
s 2 s 1
s 2 ", telling whether dwarf s 1 s 1 is smaller or taller than dwarf s 2 s 2 . s 1 s 1 and s 2 s 2 are two different dwarf names. A dwarf name consists of at most 20 20 letters from "A" to "Z" and "a" to "z". A dwarf name does not contain spaces. The number of dwarves does not exceed 1 0 4 10 4 . Output Print "impossible" if the statements are not consistent, otherwise print "possible". Example Inputcopy Outputcopy 3 Dori > Balin Balin > Kili Dori < Kili impossible
Inputcopy Outputcopy 3 Dori > Balin Balin > Kili Dori > Kili possible Bu sualda #include <bits/stdc++.h>
using namespace std;
#define ll long long #define ld long double #define endl '\n' #define pb push_back #define speedyboy ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) x.begin() , x.end() #define F first #define S second #define pll pair<ll , ll> #define pss pair<string , string> #define YES cout << "YES" << endl; #define NO cout << "NO" << endl;
const ll sz = 5e5 + 5 , INF = 1e18 , MOD = 998244353; map<string , vector> g; map<string , ll> color; vector v;
void dfs(string n){ color[n] = 1; for(auto i : g[n]){ if(color[i] == 1){ cout << "impossible" << endl; exit(0); } else if(color[i] == 0){ dfs(i); } } color[n] = 2; }
ll i , j , k; void solve(){ string s1 , s2; ll n; char c; cin >> n; for(i = 1 ; i <= n ; i++){ cin >> s1 >> c >> s2; v.pb(s1); v.pb(s2); if(c == '>'){ g[s1].pb(s2); } else{ g[s2].pb(s1); } } for(string i : v){ if(!color[i]){ dfs(i); } } cout << "possible" << endl; }
signed main(){ speedyboy; ll t = 1; //cin >> t; while(t--){ solve(); } } Bu kod nə edir?Cycle görən kimi impossible edir?
- Assistant
Bəli, sizin təqdim etdiyiniz C++ kodu doğru şəkildə işləyir və problemlə əlaqəli düzgün funksionallığı həyata keçirir. Gəlin kodun nə etdiyini və necə işlədiyini daha ətraflı izah edək.
Problemin Təhlili
Şöbə arasında verilmiş müqayisələr əsasında, əgər hər hansı bir dövriyyət (cycle) varsa, bu, bəyanatların uyğunsuz olduğunu göstərir və ən azı bir qırilənin yalan danışdığı qənaətinə gəlmək olur. Əks halda, bəyanatlar uyğundur və "possible" nəticəsi verilir.