Submission #52618619


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define task "test"
const int N = 2e5+1;
typedef pair<int,int> pi;
void __print(int x)
{
    cerr << x;
}
void __print(long x)
{
    cerr << x;
}
void __print(long long x)
{
    cerr << x;
}
void __print(unsigned x)
{
    cerr << x;
}
void __print(unsigned long x)
{
    cerr << x;
}
void __print(unsigned long long x)
{
    cerr << x;
}
void __print(float x)
{
    cerr << x;
}
void __print(double x)
{
    cerr << x;
}
void __print(long double x)
{
    cerr << x;
}
void __print(char x)
{
    cerr << '\'' << x << '\'';
}
void __print(const char *x)
{
    cerr << '\"' << x << '\"';
}
void __print(const string &x)
{
    cerr << '\"' << x << '\"';
}
void __print(bool x)
{
    cerr << (x ? "true" : "false");
}

template<typename T, typename V>
void __print(const pair<T, V> &x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template<typename T>
void __print(const T &x)
{
    int f = 0;
    cerr << '{';
    for (auto &i: x) cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print()
{
    cerr << "]\n";
}
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v)) cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

int n,m;
int par[N];
int findd(int u)
{

    if(par[u]<0) return u;
    return par[u]=findd(par[u]);
}
bool join(int u,int v)
{

    int cha_u = findd(u);
    int cha_v = findd(v);

    if(cha_u == cha_v) return false;

    return true;
}

long long get(int u)
{
    int v = findd(u);
    return -par[v];
}
long long  ans = 0;
void solve()
{
    cin >> n >> m;

    for(int i=1; i<=n; i++) par[i]=-1;
    for(int i=1; i<=m; i++)
    {
        int a,b;
        cin >> a >> b;

        if(join(a,b)){
          // cout << a << " " << b << '\n';
           int cnt_a = get(a);
           int cnt_b = get(b);

           long long val = 1LL*(cnt_a-1)*cnt_b;
           long long val1 = 1LL*(cnt_b-1)*cnt_a;
         //  cout << a << " " << b << " " << ans << '\n';
           //cout << a << " " << b << " " << cnt_a << " " << cnt_b << '\n';

           ans+= max(val+cnt_b-1,val1+cnt_a-1);


           int u =findd(a);
           int v = findd(b);

           if(par[u]<par[v]) swap(u,v);

           par[v]+=par[u];
           par[u]=v;
        }
        else {
            ans--;
        }
    }
    cout << ans;


}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if(fopen(task".inp","r"))
    {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    int t;
    t=1;
    while(t-->0)
    {
        solve();
    }

}

Submission Info

Submission Time
Task D - New Friends
User Slazenger
Language C++ 20 (gcc 12.2)
Score 350
Code Size 2933 Byte
Status AC
Exec Time 23 ms
Memory 4404 KiB

Compile Error

Main.cpp: In function ‘int main()’:
Main.cpp:164:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  164 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:165:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  165 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 350 / 350
Status
AC × 3
AC × 36
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
random_01.txt AC 23 ms 4288 KiB
random_02.txt AC 19 ms 3948 KiB
random_03.txt AC 14 ms 4288 KiB
random_04.txt AC 14 ms 4248 KiB
random_05.txt AC 22 ms 4228 KiB
random_06.txt AC 19 ms 3912 KiB
random_07.txt AC 9 ms 4292 KiB
random_08.txt AC 6 ms 4128 KiB
random_09.txt AC 12 ms 3596 KiB
random_10.txt AC 8 ms 3500 KiB
random_11.txt AC 5 ms 3504 KiB
random_12.txt AC 1 ms 3496 KiB
random_13.txt AC 13 ms 3508 KiB
random_14.txt AC 5 ms 3512 KiB
random_15.txt AC 2 ms 3592 KiB
random_16.txt AC 4 ms 3596 KiB
random_17.txt AC 13 ms 3508 KiB
random_18.txt AC 1 ms 3596 KiB
random_19.txt AC 18 ms 4404 KiB
random_20.txt AC 6 ms 3732 KiB
random_21.txt AC 18 ms 4292 KiB
random_22.txt AC 12 ms 4136 KiB
random_23.txt AC 19 ms 4228 KiB
random_24.txt AC 19 ms 4188 KiB
random_25.txt AC 22 ms 4368 KiB
random_26.txt AC 8 ms 3704 KiB
random_27.txt AC 17 ms 4280 KiB
random_28.txt AC 1 ms 4156 KiB
random_29.txt AC 1 ms 3944 KiB
random_30.txt AC 1 ms 3508 KiB
random_31.txt AC 1 ms 3512 KiB
random_32.txt AC 1 ms 3592 KiB
random_33.txt AC 1 ms 3512 KiB
sample_01.txt AC 1 ms 3624 KiB
sample_02.txt AC 1 ms 3620 KiB
sample_03.txt AC 1 ms 3576 KiB