Go: package main

import (
    "fmt"
    "bufio"
    "strconv"
    "os"
    "strings"
    "math"
)

var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
    buf := make([]byte, 0, 1000000)
    for {
        l, p, e := rdr.ReadLine()
        if e != nil {
            panic(e)
        }
        buf = append(buf, l...)
        if !p {
            break
        }
    }
    return string(buf)
}

func toInt(str string) int  {
    n, _ := strconv.Atoi(str)
    return n
}

func abs(a int) int {
    return int(math.Abs(float64(a)))
}

func getPar(x int, parent[1000]int) int {
    if parent[x] == x {
        return x
    } else {
        parent[x] = getPar(parent[x], parent)
        return parent[x]
    }
}

func main() {
    nm := strings.Split(readLine(), " ")
    n := toInt(nm[0])
    m := toInt(nm[1])
    var parent [1000]int
    for i := 0;i < n; i++ {
        parent[i] = i
    }


    for i := 0; i < m; i++ {
        ab := strings.Split(readLine(), " ")
        pa := getPar(toInt(ab[0]) - 1, parent)
        pb := getPar(toInt(ab[1]) - 1, parent)
        parent[pb] = pa
    }

    cities := make(map[int]int)
    villageCnt := 0
    for i := 0;i < n; i ++ {
        pi := getPar(i, parent)
        if i != pi {
            cities[pi] = 1
        } else {
            villageCnt ++
        }
    }
    cityCnt := len(cities)
    fmt.Println(abs(cityCnt * 2 - villageCnt))
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
struct unionfind{
	int par[1000],sz[1000];
	void init(int N){rep(i,N) par[i]=i,sz[i]=1;}
	int find(int x){
		if(par[x]==x) return x;
		return par[x]=find(par[x]);
	}
	bool same(int x,int y){
		return find(x)==find(y);
	}
	void unite(int x,int y){
		x=find(x),y=find(y);
		par[y]=x;
		sz[x]+=sz[y];
	}
}UF;
int main(){
	int N,M;
	cin>>N>>M;
	UF.init(N);
	rep(i,M){
		int a,b;
		cin>>a>>b;
		a--,b--;
		UF.unite(a,b);
	}
	int x=0;
	rep(i,N) if(UF.find(i)==i){
		if(UF.sz[i]==1) x++;
		else x--;
	}
	cout<<abs(x)<<endl;
}

C++: #include<iostream>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pro 10000
using namespace std;

int abs(int n){
	if(n>0)return n;
	else return -1*n;
}

int main(){
	int vi, town, info;
	while(cin >> vi >> info){
		town = 1;
		int check[1005] = {0};
		rep(i,info){
			int a, b;
			cin >> a >> b;
			if(check[a] == check[b] && check[a] == 0){
				check[a] = check[b] = town;
				town++;
			}else if( (check[a] != check[b]) && (check[a] == 0 || check[b] == 0) ){
				if(check[a] == 0){
					check[a] = check[b];
				}else{
					check[b] = check[a];
				}
			}else if(check[a] != check[b]){
				for(int i = 0; i < 1000; i++){
					if(check[i] == check[a]) check[i] = check[b];
				}
			}
		}
		int m = 0, c = 0;
		for(int i = 1; i <= vi; i++){
			if(check[i] != 0 && check[i] != -1){
				c++;
				for(int j = i + 1; j <= vi; j++){
					if(check[j] == check[i]) check[j] = -1;
				}
			}else if(check[i] == 0){
				m++;
			}
		}
		cout << abs(c - m) << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <utility>
#define rep(i,l,n) for(lint i=l;i<n;i++)
#define rer(i,l,n) for(lint i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int par[10000];
int _rank[10000];
	//n????´???§?????????
void init(int n){
	rep(i,0,n){
		par[i]=i; _rank[i]=0;
	}
}
	//??¨??????????±???????
int find(int x){
	if(par[x]==x) return x;
	else return par[x]=find(par[x]);
}
	//x??¨y????±??????????????????????
void unite(int x,int y){
	x=find(x);
	y=find(y);
	if(x==y) return;
	if(_rank[x]<_rank[y]) par[x]=y;
	else{
		par[x]=y;
		if(_rank[x]==_rank[y]) _rank[x]++;
	}
}
	//x??¨y???????????????????±????????????????
bool same(int x,int y){
	return find(x)==find(y);
}


int main(){
	int n,m;
	cin>>n>>m;
	init(n+1);
	vi d(n+1,0);
	int a,b,city=0,mura=n;
	rep(i,0,m){
		cin>>a>>b;
		/*if(d[a]==0 && d[b]==0) city++;
		if(d[a] && d[b]) city--;*/
		unite(a,b);
		if(d[a]==0){
			d[a]=1; mura--;
		}
		if(d[b]==0){
			d[b]=1; mura--;
		}
	}
	vi ct(n+1);
	rep(i,1,n+1){
		int tmp=find(i);
		//o(tmp);
		if(ct[tmp]==0){
			ct[tmp]=1;
			city++;
		}
	}
	o((int)abs(city-2*mura));
}

C++: #include <iostream>
#include <cstdlib>
using namespace std;
int main(){

  int n,m,c[1001]={},flag[1001]={},r0=0,r1=0;
  cin>>n>>m;
  for(int i=1,a,b;i<=m;i++){
    cin>>a>>b;
    if(!c[a]&&!c[b]){
      c[a]=i;
      c[b]=i;
    }
    if(c[a]&&!c[b]) c[b]=c[a];
    if(c[b]&&!c[a]) c[a]=c[b];
    if(c[a]&&c[b]){
      int tmp=c[b];
      for(int j=1;j<=n;j++) if(c[j]==tmp) c[j]=c[a];
    }
  }
  for(int i=1;i<=n;i++){
    if(!c[i]) r1++;
    else if(!flag[c[i]]){
      flag[c[i]]=1;
      r0++;
    }
  }
  cout<<abs(r0-r1)<<endl;
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
typedef ll int__;
#define rep(i,j) for(int__ i=0;i<(int__)(j);i++)
#define repeat(i,j,k) for(int__ i=(j);i<(int__)(k);i++)
#define all(v) v.begin(),v.end()

struct UnionFind{
    int n;
    vector<int> p;
    UnionFind(int nn) : n(nn+1){
        p.resize(n);
        rep(i, n) p[i] = i;
    }
    int root(int x){
        if(p[x] == x) return x;
        else return p[x] = root(p[x]);
    }
    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if(x != y) p[y] = x;
    }
    bool query(int x, int y){
        return root(x) == root(y);
    }
};


int main(){
    int N, M; cin >> N >> M;
    UnionFind ut(N);
    set<int> absorbed;
    rep(i, M){
        int a, b; cin >> a >> b;
        a--; b--;
        absorbed.insert(a);
        absorbed.insert(b);
        ut.unite(a, b);
    }
    set<int> city;
    rep(i, N){
        if(ut.root(i) != i) city.insert(ut.root(i));
    }
    int villedge = N - absorbed.size();
    cout << abs(villedge - (int)city.size()) << endl;

    return 0;
}

C++: #include<iostream>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pro 10000
using namespace std;

int parent[pro+1];

int find(int n){
	if(n==parent[n])return n;
	else find(parent[n]);
}

int abs(int n){
	if(n>0)return n;
	else return -1*n;
}

int main(){
	int n,m,city=0,town=0;
	cin>>n>>m;
	int a,b;
	loop(i,1,n+1)parent[i]=i;
	rep(i,m){
		cin>>a>>b;
		parent[b]=find(a);
	}
	vector<int> vec[pro+1];
	loop(i,1,n+1){
		vec[parent[i]].push_back(i);
	}
	loop(i,1,n+1){
		if(vec[i].size()==1)town++;
		if(vec[i].size()>1) city++;
	}
	cout<<abs(city-town)<<endl;
	return 0;
}