Submission #1370839


Source Code Expand

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define f first
#define s second
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ll long long
#define ld double
#define ull unsigned long long
#define PI pair < int, int > 

const int N = 2e5 + 123;
const int M = 123;
const ld Pi = acos(-1);
const ll Inf = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 7;
const int Sz = 501;
const int MOD = 1e9 + 7;

void add(int &a, int b) {
  a += b;
  if (a >= mod) a -= mod;
}
int mult(int a, int b) {
  return 1ll * a * b % mod;
}
int sum(int a, int b) {
  add(a, b);
  return a;
}

int n, m, c[N];
vector < int > g[N];
bool used[N], fail;

void dfs(int v, int col) {
  c[v] = col;
  used[v] = 1;
  for (auto to : g[v]) {
    if (used[to]) {
      if (col == c[to]) fail = 1;
    } else dfs(to, col ^ 1);
  }
}
void solve() {
  cin >> n >> m;
  for (int i = 1;i <= m;i++) {
    int a, b; cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  int x = 0, y = 0, z = 0;
  for (int i = 1;i <= n;i++) {
    if (used[i]) continue;
    if (g[i].size() == 0) {
      z++;
      continue;
    }
    fail = 0;
    dfs(i, 0);
    if (!fail) x++;
    else y++;
  }
//  cout << x << " " << y << " " << z << endl;
  ll ans = 1ll * x * x * 2 + 1ll * 2 * x * y + 1ll * y * y;
  ans += 1ll * z * z;
  ans += 2ll * (n - z) * z;
  cout << ans << endl;
}

int main() {
  srand(time(0));
  #ifdef wws
   freopen("in", "r", stdin);
   //freopen("out", "w", stdout);
  #endif 
  //ios_base::sync_with_stdio(0);
  int tt = 1;
  while(tt--) solve();
  return 0;
}

Submission Info

Submission Time
Task C - Squared Graph
User SmallBoy
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2097 Byte
Status AC
Exec Time 181 ms
Memory 12416 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 2
AC × 32
Set Name Test Cases
Sample sample1.txt, sample2.txt
All sample1.txt, sample2.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in3.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt
Case Name Status Exec Time Memory
in1.txt AC 3 ms 4992 KB
in10.txt AC 3 ms 4992 KB
in11.txt AC 3 ms 4992 KB
in12.txt AC 47 ms 8576 KB
in13.txt AC 47 ms 8576 KB
in14.txt AC 96 ms 8576 KB
in15.txt AC 10 ms 5760 KB
in16.txt AC 69 ms 7040 KB
in17.txt AC 51 ms 8064 KB
in18.txt AC 127 ms 9472 KB
in19.txt AC 93 ms 9472 KB
in2.txt AC 3 ms 4992 KB
in20.txt AC 95 ms 9472 KB
in21.txt AC 179 ms 12416 KB
in22.txt AC 138 ms 8320 KB
in23.txt AC 109 ms 7168 KB
in24.txt AC 4 ms 5504 KB
in25.txt AC 181 ms 12416 KB
in26.txt AC 3 ms 4992 KB
in27.txt AC 4 ms 4992 KB
in28.txt AC 180 ms 12416 KB
in3.txt AC 3 ms 4992 KB
in4.txt AC 3 ms 4992 KB
in5.txt AC 139 ms 8192 KB
in6.txt AC 9 ms 5760 KB
in7.txt AC 9 ms 5632 KB
in8.txt AC 18 ms 6272 KB
in9.txt AC 55 ms 7424 KB
sample1.txt AC 3 ms 4992 KB
sample2.txt AC 3 ms 4992 KB