QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#526979 | #1957. Friendship Graphs | sroid_03# | WA | 531ms | 105516kb | C++20 | 12.7kb | 2024-08-22 04:21:19 | 2024-08-22 04:21:20 |
Judging History
answer
#pragma GCC optimize("O1")
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template<class T>using ind_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class T>using ind_mset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
typedef unsigned long long ull;
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<pll> vpll;
typedef vector<vl> vll;
template<class T> using maxpq = priority_queue<T>;
template<class T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template<class T> using vec1 = vector<T>;
template<class T> using vec2 = vector<vector<T>>;
template<class T> using vec3 = vector<vector<vector<T>>>;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i,b,a) for(ll i =((b)-1);i>=(a);i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(),(v).rend()
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fbo(a) find_by_order(a) //will give a-th largest element
#define ook(a) order_of_key(a) //will give no. of elements strictly lesser than a
#define sz(x) ((int)(x).size())
#define nzl(x) __builtin_clzll(x)
#define nzr(x) __builtin_ctzll(x)
#define setbits(x) __builtin_popcountll(x)
#define setbitsParity(x) __builtin_parityll(x) // 1 -> odd else 0 if even
#define umap unordered_map
#define uset unordered_set
#define nl "\n"
#define PI atan(1)*4
#define E 2.71828
#define yes {cout << "Yes" << endl; return;}
#define no {cout << "No" << endl; return;}
#define YES {cout << "Yes" << endl;}
#define NO {cout << "No" << endl;}
#define nyet {cout<<"-1"<<endl;return;}
#define mxe(v) (*max_element(v.begin(),v.end()))
#define mne(v) (*min_element(v.begin(),v.end()))
#define unq(v) v.resize(distance(v.begin(), unique(v.begin(), v.end())));
#define ub upper_bound
#define lb lower_bound
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()
#define outt(a) \
FOR(i,1,sz(a)) \
cout << a[i] << " "; \
cout << endl;
#define inn(a) \
FOR(i,1,sz(a)) \
cin>>a[i];
#define FAST_AF_BOI \
ios_base ::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;//abk
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
typedef gp_hash_table<long long,long long,custom_hash> fast_map;
typedef unordered_map<long long,long long,custom_hash> safe_map;
// ================================== i/o module ==================================
template <class T> void _print(T x){cerr<<x;};
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template<class T>void read(T &x){x=0;int f=0;char ch=getchar();while(ch<'0' || ch>'9')f|=(ch=='-'),ch=getchar();while(ch>='0' && ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();x=f? -x:x;return ;}
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
template <class T> inline vector<T>& operator--(vector<T>& v) { for(auto &x:v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { for(auto &x:v) ++x; return v; }
template <class T> inline vector<T>& operator^=(vector<T>& v,int y) { for(auto &x:v) x^=y; return v; }
inline string& operator^=(string& s,int y) { for(auto &x:s)x=((x-'0')^y)+'0' ; return s; }
void dgb_out () { cerr << endl; }
template < typename Head, typename... Tail >
void dgb_out ( Head H, Tail... T) { cerr <<' ' << H; dgb_out (T...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dgb_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
// ================================================================================
//`````````````````````````````````````````````IMP FUNCTIONS``````````````````````````````````````````````````````
ll ceil(ll a,ll b){return (a+b-1)/b;}
int log_2(ull i){return i?nzl(1)-nzl(i):-1;}
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll bin_expo(ll a, ll b, ll mod) {ll res = 1;a%=mod;if(a==0)return 0;while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll bin_mul(ll a, ll b, ll mod) {ll res = 0; while (b > 0) {if (b & 1)res = (res + a) % mod; a = (a + a) % mod; b = b >> 1;} return res;}
void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return bin_expo(a, b - 2, b);}
ll ncr(ll n, ll r, ll m, ll *fact, ll *ifact) {if(n<r)return 0;ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r];if(n < r) return 0;if(n == r || r == 0) return 1;if(r<0) return 0; return (((val1 * val2) % m) * val3) % m;}
void google(int t) {cout << "Case #" << t << ": ";}
vl sieve(int n) {int*arr = new int[n + 1](); vl vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} //only for prime m
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i*i <= n; i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
ll large_expo(ll a,ll b,ll c,ll m) {return bin_expo(a,bin_expo(b,c,phin(m)),m);} //(a^b^c)%M
ll large_expo_prime(ll a,ll b,ll c,ll m) {return bin_expo(a,bin_expo(b,c,m-1),m);} //(a^b^c)%M when m is prime
template<class T>vector<T> prefixSum(vector<T> v, bool flag){vector<T> ans;T sum = 0;if (flag){for (auto &e : v){sum += e;ans.eb(sum);}}else{ans.pb(0);REP(i, v.size(), 0){sum += v[i];ans.eb(sum);}reverse(all(ans));}return ans;}
ll ffs(ll n){if(n==0)return -1;return log2(n & -n);}
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll uid(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng);}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
const ll INF = 1e18 + 10;
const ll inf = 1e9 + 10;
const ll N = 30000 + 10;
const ll mod1 = (1e9 + 7);
const ll mod2 = (998244353);
/*
Self notes cause I need it :
1. dumbfk read the question properly ... I m fed up with this lack of presence of mind
2. dont assume that something is given in question ,like the input is sorted or stuff
if some step needs the input to be sorted , either check question or sort , dont assume
3. in case you have less time to debug :
Common errors :
-> Wrote wrong variable names at places cause ur stupid
-> For debugging u hard coded small values and submitted that solution right away
*/
#define int long long
///Zero Indexed
///F=(x_0 or y_0) and (x_1 or y_1) and ... (x_{vars-1} or y_{vars-1})
///here y_i belongs to x_i
///is there any assignment of x_i such that F=true
///for (x_0 xor y_0) and (x_1 xor y_1)...
///replace (x_i xor y_i) by (x_i or y_i) and (not x_i or not y_i)
struct twosat {
int n; /// total size combining +, -. must be even.
vector< set<int> > g, gt; /// graph, transpose graph
vector<bool> vis, res; /// visited and resulting assignment
vector<int> comp; /// component number
stack<int> ts; /// topsort
twosat(int vars=0) {
n = (vars << 1);
g.resize(n);
gt.resize(n);
}
void addEdge(int u,int v){
g[u].insert(v);
gt[v].insert(u);
}
void dfs1(int u) { //topo
vis[u] = true;
for(int v : g[u]) if(!vis[v]) dfs1(v);
ts.push(u);
}
void dfs2(int u, int c) { //scc
comp[u] = c;
for(int v : gt[u]) if(comp[v] == -1) dfs2(v, c);
}
bool ok() {
vis.resize(n, false); //find the topo
for(int i=0; i<n; ++i) if(!vis[i]) dfs1(i);
int scc = 0; //find the sccs
comp.resize(n, -1);
while(!ts.empty()) {
int u = ts.top();
ts.pop();
if(comp[u] == -1) dfs2(u, scc++);
}
res.resize(n/2);
for(int i=0; i<n; i+=2) {
if(comp[i] == comp[i+1]) return false;
res[i / 2] = comp[i] > comp[i + 1];
}
return true;
}
};
//for same -> st.add(u,0,v,1),st.add(u,1,v,0) as !u or v == u->v
//for opp -> st.add(u,0,v,0),st.add(u,1,v,1) as u or v == !u->v
void transcendent(int tc)
{
int n, m; cin >> n >> m;
vec2<int> ok(n, vec1<int>(n));
FOR(_, 0, m) {
int u, v; cin >> u >> v;
-- u, -- v;
ok[u][v] = ok[v][u] = 1;
}
if(m == n * (n - 1) / 2) {
cout << (n & 1) << nl;
return;
}
twosat st(n);
FOR(i, 0, n) {
FOR(j, i + 1, n) {
if(!ok[i][j]) {
st.addEdge(i << 1, j << 1 | 1);
st.addEdge(i << 1 | 1, j << 1);
st.addEdge(j << 1, i << 1 | 1);
st.addEdge(j << 1 | 1, i << 1);
}
}
}
if(!st.ok()) nyet
vec1<int> a, b;
FOR(i, 0, n) {
if(st.res[i]) a.pb(i);
else b.pb(i);
}
if(sz(a) > sz(b)) swap(a, b);
int cnt = 0;
for(auto &x : b) {
bool hv = true;
for(auto &y : a) {
hv &= ok[x][y];
}
cnt += hv;
}
int sz1 = sz(a), sz2 = sz(b);
int can = min(cnt, (sz2 - sz1) / 2);
sz1 += can;
sz2 -= can;
cout << sz2 - sz1 << nl;
}
static void read(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
int32_t main()
{
//read();
FAST_AF_BOI
auto begin = std::chrono::high_resolution_clock::now();
cout << fixed << setprecision(12);
cerr << fixed << setprecision(0);
//clock_t timer;
//timer = clock();
//PreComp();
int test = 1;
// cin >> test;
FOR(tc, 1, test + 1)
{
//cerr << endl << "----Test:" << tc << "----" <<endl;
transcendent(tc);
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
//cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 63ms
memory: 11956kb
input:
1000 499000 20 615 260 390 779 37 13 563 650 605 358 684 783 370 162 90 379 662 88 208 458 371 178 3 590 762 455 514 738 641 270 805 205 881 205 315 837 54 976 579 519 532 982 669 563 804 648 274 268 293 182 392 337 772 961 603 294 607 546 772 189 218 702 266 515 610 691 965 643 235 509 193 184 302 ...
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 56ms
memory: 11900kb
input:
1000 498999 35 65 880 835 501 309 590 758 882 857 356 493 43 623 644 637 361 785 58 317 26 11 595 521 723 629 611 36 789 29 30 650 426 475 852 862 667 137 677 173 656 44 457 279 680 567 789 989 368 873 510 721 128 584 835 956 419 779 607 568 317 790 932 580 336 400 74 265 772 855 939 816 448 883 381...
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 73ms
memory: 11956kb
input:
1000 498998 667 721 880 868 426 900 882 839 789 440 590 395 356 847 852 758 35 648 26 723 611 329 644 560 723 45 595 813 501 338 58 762 30 302 43 340 361 734 74 863 128 433 656 196 677 188 932 651 835 603 368 568 336 105 317 225 457 350 419 771 607 545 789 31 772 465 510 542 680 888 504 445 884 999 ...
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 73ms
memory: 11632kb
input:
1000 499499 552 449 897 36 561 770 188 194 233 385 689 608 814 604 386 789 440 778 51 295 368 726 835 647 182 31 387 250 202 887 607 184 189 192 54 774 252 403 562 109 878 528 258 449 823 460 619 906 952 96 69 383 630 81 474 996 273 651 749 270 682 976 147 209 287 612 402 108 575 479 864 462 1000 72...
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 62ms
memory: 12128kb
input:
1000 498751 681 409 733 39 732 717 449 73 41 314 80 971 61 44 139 989 93 338 235 188 113 955 362 380 471 183 228 566 120 625 543 1 450 262 411 197 470 342 461 616 704 186 345 782 499 672 391 288 541 531 917 742 736 819 345 428 821 265 321 789 270 742 452 609 386 668 60 440 421 23 386 677 167 69 944 ...
output:
498
result:
ok single line: '498'
Test #6:
score: 0
Accepted
time: 58ms
memory: 11976kb
input:
1000 499000 679 199 582 83 847 334 396 135 314 922 406 13 371 181 325 954 279 841 428 913 363 248 750 509 283 910 403 697 298 213 56 57 172 914 89 580 203 814 499 491 875 236 846 806 434 31 990 138 883 476 441 309 67 662 829 2 619 824 418 151 788 630 194 350 81 484 548 497 892 45 684 818 516 387 873...
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 71ms
memory: 12228kb
input:
1000 498501 590 631 599 460 689 534 154 899 941 870 605 576 2 374 343 119 264 170 866 139 907 319 256 962 937 684 227 461 805 523 640 16 939 858 593 485 501 791 426 556 596 682 524 5 79 143 782 491 659 211 608 362 85 881 361 867 791 509 601 9 658 609 268 519 391 379 231 580 100 674 668 219 203 778 1...
output:
998
result:
ok single line: '998'
Test #8:
score: 0
Accepted
time: 521ms
memory: 105420kb
input:
1000 249500 596 90 14 715 205 280 125 829 988 726 888 600 689 300 576 837 407 955 473 827 181 232 871 719 528 529 981 53 348 17 245 859 135 52 98 955 671 45 735 48 612 327 122 141 697 215 527 884 31 16 408 262 826 153 464 675 526 344 744 739 838 337 860 9 988 526 498 101 324 212 752 129 119 191 660 ...
output:
0
result:
ok single line: '0'
Test #9:
score: 0
Accepted
time: 531ms
memory: 105516kb
input:
1000 249499 473 827 576 837 205 280 407 955 14 715 125 829 988 726 596 90 689 300 888 600 981 53 671 45 245 859 181 339 98 955 871 719 528 529 135 52 735 48 348 379 612 857 31 327 408 706 464 675 527 884 122 141 826 153 697 579 988 526 324 212 138 316 660 549 860 9 131 782 119 191 752 363 838 510 74...
output:
-1
result:
ok single line: '-1'
Test #10:
score: 0
Accepted
time: 470ms
memory: 90768kb
input:
1000 289101 851 683 314 539 40 366 43 66 782 468 282 843 284 650 455 144 416 146 694 563 879 73 619 563 229 447 472 349 659 262 792 343 690 177 565 191 844 716 756 24 728 971 911 540 675 85 934 110 496 726 956 615 713 747 252 960 77 707 247 671 53 417 334 414 713 24 269 547 399 864 160 542 761 133 3...
output:
398
result:
ok single line: '398'
Test #11:
score: 0
Accepted
time: 470ms
memory: 90644kb
input:
1000 289100 851 683 455 960 43 66 782 468 416 146 314 539 40 366 282 843 694 563 284 650 619 563 229 447 472 349 792 343 879 73 659 262 690 177 565 191 675 242 756 24 911 540 956 615 934 617 844 716 496 726 728 971 77 707 713 24 334 414 247 671 53 417 252 960 713 747 269 547 390 373 399 864 761 354 ...
output:
-1
result:
ok single line: '-1'
Test #12:
score: 0
Accepted
time: 211ms
memory: 45616kb
input:
1000 409500 606 839 517 698 761 152 582 382 302 633 725 114 261 469 104 723 34 275 336 658 786 133 243 313 367 839 422 137 440 620 597 337 581 141 84 326 791 744 122 64 56 457 784 786 199 919 235 697 41 64 235 470 439 444 858 746 830 314 998 147 367 421 201 209 326 124 831 712 625 745 453 870 414 38...
output:
800
result:
ok single line: '800'
Test #13:
score: 0
Accepted
time: 197ms
memory: 45588kb
input:
1000 409499 761 661 302 728 517 190 582 357 725 64 606 591 261 659 104 158 581 526 367 591 440 221 56 668 336 313 243 409 786 151 791 356 422 775 84 679 597 859 122 109 34 261 784 170 235 927 199 217 998 923 830 900 858 646 41 109 235 719 439 872 625 322 414 963 929 562 33 373 435 392 531 676 831 88...
output:
-1
result:
ok single line: '-1'
Test #14:
score: -100
Wrong Answer
time: 0ms
memory: 3644kb
input:
17 121 2 5 16 5 3 14 15 16 16 6 10 16 9 5 17 7 12 5 9 16 2 7 3 10 3 1 4 7 15 1 15 8 9 17 4 8 4 13 11 6 9 7 15 17 12 9 10 7 11 13 2 8 17 6 14 17 9 13 1 9 10 6 16 13 9 6 10 13 15 13 8 5 17 5 14 1 3 2 2 13 12 1 2 9 15 6 9 11 2 10 3 8 10 8 15 9 14 11 4 11 2 4 12 8 12 17 3 16 12 14 2 14 10 5 8 7 15 5 10 ...
output:
9
result:
wrong answer 1st lines differ - expected: '5', found: '9'