Submission #53126279


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//#include <atcoder/modint>
//using namespace atcoder;
//using mint = modint998244353;
# define M_PI           3.14159265358979323846  /* pi */
#define watch(x) cout << (#x) << " is " << (x) << endl
 
//#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
 
#if 0
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class K, class V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
#endif

const int MOD = (1e9+7);
template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v){ 
    for (const T &x: v) out << x << ' '; 
    return out;
}
template<class T>
void printmat(const vector<vector<T>>& mat) {
    for (auto row : mat) {
        for (auto elem : row)
            cout << elem << " ";
        cout << "\n";
    }
}
void printdq(const deque<int>& v) {
    for (auto elem : v)
        cout << elem << " ";
    cout << endl;
}
template<class T>
void printv(const vector<T>& v) {
    for (auto elem : v)
        cout << elem << " ";
    cout << "\n";
}
template<class T>
void printdq(const deque<T>& v) {
    for (auto elem : v)
        cout << elem << " ";
    cout << endl;
}
template<class T1, class T2>
void printvp(const vector<pair<T1,T2>>& vp) {
    for (auto pr : vp) {
        cout << pr.first << " " << pr.second;
        cout << "\n";
    }
}
void printvs(const vector<set<int>>& vs) {
    for (auto row : vs) {
        for (auto elem : row)
            cout << elem << ", ";
        cout << endl;
    }
}
template<class T>
void printht(const unordered_map<T, T>& ht) {
    for (auto elem : ht)
        cout << elem.first << " : " << elem.second << endl;
}
template<class T1, class T2>
void printmp(const map<T1, T2>& ht) {
    for (auto elem : ht)
        cout << elem.first << " : " << elem.second << endl;
}
template<class T>
void printst(const set<T>& st) {
    for (auto elem : st)
        cout << elem << " ";
    cout << endl;
}
template<class T>
void printms(const multiset<T>& st) {
    for (auto elem : st)
        cout << elem << " ";
    cout << endl;
}
 
bool isPrime(long long n) {
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n % 2 == 0 || n % 3 == 0)
        return false;
    for (long long i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    return true;
}
 
map<long long, long long> primeFactors(long long n) {
    map<long long, long long> ans;
    while (n % 2 == 0) {
        ans[2]++;
        n = n/2;
    }
    for (long long i = 3; i*i <= (n); i = i + 2) {
        while (n % i == 0) {
            ans[i]++;
            n = n/i;
        }
    }
    if (n > 2)
        ans[n]++;
    return ans;
}
 
/*
    vector<int> uf(n), sz(n,1);
    for (int i=0; i<n; i++) 
        uf[i] = i;
*/
int find_f(const vector<int>& uf, int i) {
    while (uf[i]!=i)
        i = uf[i];
    return i;
}
bool union_f(vector<int>& uf, vector<int>& sz, int a, int b) {
    a = find_f(uf, a);
    b = find_f(uf, b);
    //cout << "a, b = " << a << ", " << b << endl;
    if (a==b) return false;
    if (sz[a] < sz[b]) {
        //cout << "sz[a], sz[b] = " << sz[a] << ", " << sz[b] << endl;
        //cout << "a, b = " << a << ", " << b << endl;
        swap(a,b);
        //cout << "a, b = " << a << ", " << b << endl;
    }
    sz[a] += sz[b];
    uf[b] = a;
    return true;
}
 
long long modexp(long long b, long long e, long long M) {
    if (!e) return 1;
    b %= M;
    long long x = modexp(b * b % M, e / 2, M);
    if (e % 2) {
        return b * x % M;
    } else {
        return x;
    }
}


ll gcdExtended(ll a, ll b, ll* x, ll* y) {
    if (a == 0) {
        *x = 0, *y = 1;
        return b;
    }
    ll x1, y1;
    ll gcd = gcdExtended(b % a, a, &x1, &y1);
    *x = y1 - (b / a) * x1;
    *y = x1;
    return gcd;
}

ll modInverse(ll a, ll m) {
    ll x, y, res=-1;
    ll g = gcdExtended(a, m, &x, &y);
    if (g != 1) {
        //cout << "Inverse doesn't exist";
        res = -1;
    } else {
        // m is added to handle negative x
        res = (x % m + m) % m;
    }
    return res;
}

int lenOfLIS(vector<int>& v) {        
    int n = v.size(), len = 0;
    vector<int> dp(n,0);
    for (int num : v) {
        int i = lower_bound(dp.begin(), dp.begin()+len, num) - dp.begin();
        dp[i] = num;
        if (i == len) {
            len++;
        }
    }
    return len;        
}


#if 0
const int N = 1e6+4;  // limit for array size
int n;  // array size
int t[2 * N];

void build() {  // build the tree
  for (int i = n - 1; i > 0; --i) t[i] = max(t[i<<1], t[i<<1|1]);
}

void modify(int p, int value) {  // set value at position p
  for (t[p += n] = value; p > 1; p >>= 1) t[p>>1] = t[p] + t[p^1];
}

int query(int l, int r) {  // max on interval [l, r)
  int res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l&1) res = max(res, t[l++]);
    if (r&1) res = max(res, t[--r]);
  }
  return res;
}
#endif

vector<int> SieveOfEratosthenes(int n) {
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
  
    for (int p=2; p*p<=n; p++) {
        if (prime[p]) {
            for (int i=p*p; i<=n; i+=p)
                prime[i] = false;
        }
    }
    vector<int> v;
    for (int p=2; p<=n; p++)
        if (prime[p])
            v.push_back(p);
    return v;
}

vector<vector<ll>> merge(vector<vector<ll>>& intervals) {
    vector<vector<ll>> ans;
    sort(intervals.begin(), intervals.end());
    vector<ll> curr = intervals[0];
    for (int i=1; i<intervals.size(); i++) {
        if (curr[1]<intervals[i][0]) {
            ans.push_back(curr);
            curr = intervals[i];
        } else {
            curr[1] = max(curr[1], intervals[i][1]);
        }
    }
    ans.push_back(curr);
    return ans;
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);    
    int T=1, caseIdx=0;   
    //cin >> T;        
    while (T--) {
        //caseIdx++;
        //const int M = 998244353;
        ll n, m, ans=0;
        cin >> n >> m;
        vector<vector<ll>> mat(m);
        for (int i=0; i<m; i++) {
            ll k, c;
            cin >> k >> c;
            mat[i].resize(2+k);
            mat[i][0] = c;
            mat[i][1] = k;
            for (int j=2; j<2+k; j++) {
                cin >> mat[i][j];
                mat[i][j]--;
            }
        }
        sort(mat.begin(), mat.end());
        //printmat(mat);
        set<ll> st;
        vector<int> uf(n), sz(n,1);
        for (int i=0; i<n; i++) 
            uf[i] = i;

//int find_f(const vector<int>& uf, int i) 
//bool union_f(vector<int>& uf, vector<int>& sz, int a, int b)        
        
        for (int i=0; i<m; i++) {
            ll c = mat[i][0];
            ll k = mat[i][1];
            ll diffsz = 0;
            for (int j=2; j<2+k; j++) {
                if (st.count(mat[i][j])==0) {
                    diffsz++;
                    st.insert(mat[i][j]);
                }
            }
            //watch(diffsz);
            for (int j=3; j<2+k; j++) {
                int roota = find_f(uf, mat[i][j-1]);
                int rootb = find_f(uf, mat[i][j]);
                if (roota==rootb)
                    continue;
                union_f(uf, sz, mat[i][j-1], mat[i][j]);
                ans += c;
            }

        }
        
        if (st.size()<n)
            ans = -1;
        
        //cout << fixed << setprecision(9);
        cout << ans << "\n";
        //cout << "Case #" << caseIdx << ": " << ans << "\n";   
    }
}

Submission Info

Submission Time
Task E - Clique Connect
User llc5pg
Language C++ 17 (gcc 12.2)
Score 450
Code Size 8062 Byte
Status AC
Exec Time 208 ms
Memory 28248 KiB

Compile Error

Main.cpp:11: warning: ignoring ‘#pragma GCC optimization’ [-Wunknown-pragmas]
   11 | #pragma GCC optimization ("Ofast")
      | 
Main.cpp: In function ‘std::vector<std::vector<long long int> > merge(std::vector<std::vector<long long int> >&)’:
Main.cpp:239:20: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::vector<long long int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
  239 |     for (int i=1; i<intervals.size(); i++) {
      |                   ~^~~~~~~~~~~~~~~~~
Main.cpp: In function ‘int main()’:
Main.cpp:306:22: warning: comparison of integer expressions of different signedness: ‘std::set<long long int>::size_type’ {aka ‘long unsigned int’} and ‘ll’ {aka ‘long long int’} [-Wsign-compare]
  306 |         if (st.size()<n)
      |             ~~~~~~~~~^~
Main.cpp:255:14: warning: unused variable ‘caseIdx’ [-Wunused-variable]
  255 |     int T=1, caseIdx=0;
      |              ^~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 3
AC × 52
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 02_random2_20.txt, 02_random2_21.txt, 02_random2_22.txt, 02_random2_23.txt, 02_random2_24.txt, 02_random2_25.txt, 02_random2_26.txt, 02_random2_27.txt, 02_random2_28.txt, 02_random2_29.txt, 02_random2_30.txt, 02_random2_31.txt, 02_random2_32.txt, 02_random2_33.txt, 02_random2_34.txt, 02_random2_35.txt, 02_random2_36.txt, 02_random2_37.txt, 02_random2_38.txt, 02_random2_39.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3556 KiB
00_sample_01.txt AC 1 ms 3496 KiB
00_sample_02.txt AC 1 ms 3420 KiB
01_random_00.txt AC 147 ms 17952 KiB
01_random_01.txt AC 159 ms 19868 KiB
01_random_02.txt AC 172 ms 22028 KiB
01_random_03.txt AC 186 ms 24128 KiB
01_random_04.txt AC 196 ms 26848 KiB
02_random2_00.txt AC 147 ms 18396 KiB
02_random2_01.txt AC 148 ms 18328 KiB
02_random2_02.txt AC 148 ms 18408 KiB
02_random2_03.txt AC 152 ms 18336 KiB
02_random2_04.txt AC 147 ms 18464 KiB
02_random2_05.txt AC 156 ms 19608 KiB
02_random2_06.txt AC 158 ms 19572 KiB
02_random2_07.txt AC 159 ms 19796 KiB
02_random2_08.txt AC 158 ms 19692 KiB
02_random2_09.txt AC 158 ms 19636 KiB
02_random2_10.txt AC 163 ms 20892 KiB
02_random2_11.txt AC 166 ms 20972 KiB
02_random2_12.txt AC 170 ms 20896 KiB
02_random2_13.txt AC 167 ms 21136 KiB
02_random2_14.txt AC 170 ms 20960 KiB
02_random2_15.txt AC 173 ms 22324 KiB
02_random2_16.txt AC 173 ms 22280 KiB
02_random2_17.txt AC 179 ms 22300 KiB
02_random2_18.txt AC 176 ms 22276 KiB
02_random2_19.txt AC 180 ms 22276 KiB
02_random2_20.txt AC 178 ms 23532 KiB
02_random2_21.txt AC 176 ms 23680 KiB
02_random2_22.txt AC 187 ms 23644 KiB
02_random2_23.txt AC 187 ms 23648 KiB
02_random2_24.txt AC 185 ms 23684 KiB
02_random2_25.txt AC 179 ms 24932 KiB
02_random2_26.txt AC 184 ms 25004 KiB
02_random2_27.txt AC 198 ms 25000 KiB
02_random2_28.txt AC 194 ms 25020 KiB
02_random2_29.txt AC 199 ms 25004 KiB
02_random2_30.txt AC 181 ms 26584 KiB
02_random2_31.txt AC 188 ms 26608 KiB
02_random2_32.txt AC 200 ms 26588 KiB
02_random2_33.txt AC 203 ms 26584 KiB
02_random2_34.txt AC 199 ms 26656 KiB
02_random2_35.txt AC 186 ms 28248 KiB
02_random2_36.txt AC 192 ms 28092 KiB
02_random2_37.txt AC 208 ms 28156 KiB
02_random2_38.txt AC 208 ms 28128 KiB
02_random2_39.txt AC 205 ms 28100 KiB
03_handmade_00.txt AC 1 ms 3436 KiB
03_handmade_01.txt AC 65 ms 15512 KiB
03_handmade_02.txt AC 64 ms 15432 KiB
03_handmade_03.txt AC 182 ms 28248 KiB