How could this code output 2 different things? Problem: USACO 2016 December Contest, Silver, Problem 2

Problem Info

Name of problem:
Cities and States
http://www.usaco.org/index.php?page=viewproblem2&cpid=667

My Work

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;

int main()
{
ifstream fin(“citystate.in”);
ofstream fout(“citystate.out”);

unordered_map<string, set<int>> cityState;

int N;
fin >> N;

for (int i = 0; i < N; ++i)
{
	string a, b;
	fin >> a >> b;
	char a1 = a[0];
	char a2 = a[1];
	char b1 = b[0];
	char b2 = b[1];

	string s;
	s.push_back(a1);
	s.push_back(a2);
	s.push_back(b1);
	s.push_back(b2);

	cityState[s].insert(i);
}

int twoAns = 0;

for (const auto& it : cityState)
{
	string s;

	s.push_back(it.first[2]);
	s.push_back(it.first[3]);
	s.push_back(it.first[0]);
	s.push_back(it.first[1]);

	for (int element : it.second)
	{
		twoAns += cityState[s].size() - cityState[s].count(element);
	}
}

int ans = twoAns / 2;

fout << ans;

}
cpp
Add commented code here

Add explanation of code here

What I’ve Tried

I’ve ran it in tests, and it always outputted the right answer in Visual Studio, it’s a completely different story when I try to submit this. In Visual Studio, I always get that the output is 1, the correct answer, but when I submit it, I always get the error message, saying that my code outputs 0.

Question

How does this output one thing in Microsoft Visual Studio but a completely different thing in submission? I submitted the same thing multiple times and they always said that the output is 0. I always get hit with this:
Incorrect answer on sample input case – details below
Your output file citystate.out:
0

The correct answer:
1

I tried running this in Microsoft Visual Studio multiple times and it always outputted 1.

Checklist

See How to ask for help on a problem for more information.

  • Include a link to the problem
  • Format your post (especially code blocks)
  • Include what you’ve tried so far
  • Add comments to your code
  • Read the debugging module

This might be because your accessing a vector index that’s out of bounds. When that occurs, C++ might throw a random number at you instead of giving an error.

I didn’t use a vector. I also changed everything to long longs. Didn’t work. What else should I do?

Oh, sorry, I didn’t see that. Have you tried putting debug prints to see what USACO’s grader has the variables as?